一.【业务需求】有些时候一个父组件的很多方法有很多子组件都要使用
二.【代码实现】
父组件:】
methods:{
//打开模块
openModule(m, props) {
var index = this.moduleIndex(m.component);
if (index >= 0) {
this.active = index;
return;
}
var p = this.modules;
var item = {
component: m.component,
title: m.title,
props: props
};
p.push(item);
this.$refs.navbar.addTab(m.title);
this.active = p.length - 1;
},
}
created: function() {
//把父组件定义为全局
Vue.prototype.$main=this;
},
子组件:】
子组件调用父组件的方法
this.$main.openModule({
component: "AddInputInformation", title: "新增存款信息"
},'title');