很久沒有寫博客了,今天看到掘金有人寫總結下Vue組件方法標題,回想了一下總結的知識,感受本身有些遺忘,就寫下此篇複習下,結果發現以前總結方法都能寫出來。這些比較之前的了,最近又很久沒學習。若是還有什麼其餘方法,求賜教。代碼是純Markdown寫的,可能代碼會寫錯。若是看到的也麻煩提個醒vue
父組件vuex
<template>
<div>
<child></child>
</div>
</template>
<script>
import child from '@/components/child';
export default {
components: {
child
},
methods: {
fatherMethod() {
console.log('father組件');
}
}
}
</script>
複製代碼
子組件學習
<template>
<div @click="activeBtn"> </div>
</template>
<script>
export default {
methods: {
activeBtn() {
this.$parent.fatherMethod()
}
}
}
</script>
複製代碼
父組件this
<template>
<div>
<child @callFather="activeSon"></child>
</div>
</template>
<script>
import child from '@/components/child';
export default {
components: {
child
},
methods: {
fatherMethod() {
console.log('father組件');
},
activeSon(){
this.fatherMethod()
}
}
}
</script>
複製代碼
子組件spa
<template>
<div @click="activeBtn"> </div>
</template>
<script>
export default {
methods: {
activeBtn() {
this.$emit("callFather")
}
}
}
</script>
複製代碼
父組件code
<template>
<div>
<child :activeSon="fatherMethod()"></child>
</div>
</template>
<script>
import child from '@/components/child';
export default {
components: {
child
},
methods: {
fatherMethod() {
console.log('father組件');
}
}
}
</script>
複製代碼
子組件component
<template>
<div @click="activeBtn"> </div>
</template>
<script>
export default {
props:{
activeSon: {
type: Function,
default: null
}
},
methods: {
activeBtn() {
this.activeSon()
}
}
}
</script>
複製代碼