Vue中把一个组件的方法定义为全局方法、子组件控制父组件增加tab页面(一)

一.【业务需求】有些时候一个父组件的很多方法有很多子组件都要使用

二.【代码实现】
父组件:】
 


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');

猜你喜欢

转载自blog.csdn.net/weixin_43837268/article/details/84674833
今日推荐