父组件
<template>
<p>
<child :fatherMethod="fatherMethod"></child>
</p>
</template>
<script>
import child from '~/components/dam/child';
export default {
components: {
child
},
methods: {
fatherMethod() {
console.log('测试');
}
}
};
</script>
子组件
<template>
<p>
<button @click="childMethod()">点击</button>
</p>
</template>
<script>
export default {
props: {
fatherMethod: {
type: Function,
default: null
}
},
methods: {
childMethod() {
if (this.fatherMethod) {
this.fatherMethod();
}
}
}
};
</script>
文章讲述了在Vue.js中,如何通过父子组件间的props和methods实现父组件的`fatherMethod`函数在子组件被点击时被调用。通过`<childfatherMethod=fatherMethod></child>`的模板语法展示,讲解了组件之间的通信机制。
638

被折叠的 条评论
为什么被折叠?



