Vue中子组件调用父组件的方法,这里有三种方法提供参考
第一种方法是直接在子组件中通过this.$parent.event来调用父组件的方法
父组件
-
-
-
-
-
-
-
import child from ‘~/components/dam/child‘;
-
-
-
-
-
-
-
-
-
-
-
子组件
-
-
-
<button @click="childMethod()">点击</button>
-
-
-
-
-
-
-
this.$parent.fatherMethod();
-
-
-
-
第二种方法是在子组件里用$emit
向父组件触发一个事件,父组件监听这个事件就行了。
父组件
-
-
-
<child @fatherMethod="fatherMethod"></child>
-
-
-
-
import child from ‘~/components/dam/child‘;
-
-
-
-
-
-
-
-
-
-
-
子组件
-
-
-
<button @click="childMethod()">点击</button>
-
-
-
-
-
-
-
this.$emit(‘fatherMethod‘);
-
-
-
-
第三种是父组件把方法传入子组件中,在子组件里直接调用这个方法
父组件
-
-
-
<child :fatherMethod="fatherMethod"></child>
-
-
-
-
import child from ‘~/components/dam/child‘;
-
-
-
-
-
-
-
-
-
-
-
子组件
-
-
-
<button @click="childMethod()">点击</button>
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
转:https://blog.csdn.net/zgrkaka/article/details/100528714
Vue子组件调用父组件的方法
原文:https://www.cnblogs.com/ygyy/p/13396188.html