组件brother2想调用组件brother1的方法,并传参
可以新建一个handler.js文件
import Vue from 'vue';
export default new Vue();
brother1组件方法:
import handler from './handler';
methods: {
handleEmit () {
console.log('tabNum',this.tabNum)
handler.$emit('changeActiveItem', this.tabNum);
}
}
brother2组件调用:
import handler from './handler'
mounted() {
// 当页面渲染完成后,监听brother1兄弟组件传递过来的tabNum值
// 当前组件要保证与brother1兄弟组件的页面tab值保持一致
handler.$on('changeActiveItem', (tabNum) => {
console.log('传过来的值',tabNum)
});
},