vue 子组件调用父组件方法
有三种方法:
this.$parent.fatherMethod();
直接通过$parent
调用父组件方法。- 子组件用
$emit
向父组件出发一个事件,父组件监听该事件。childMethod() { // 子组件 this.$emit('fatherMethod'); } // 父组件 <child @fatherMethod="fatherMethod"></child>
- 父组件将方法传入子组件,子组件调用即可。
childMethod() { // 子组件 this.$emit('fatherMethod'); } // 父组件 <child :fatherMethod="fatherMethod"></child> props: { fatherMethod: { type: Function, default: null } }, methods: { childMethod() { if (this.fatherMethod) { this.fatherMethod(); } } }
有什么不同见解可以在评论区共同讨论