vue-cli bus组件之间传值

1.在main.js中注册全局的bus 

Vue.prototype.bus=new Vue();

2.在组件中使用

// 子组建使用:this.bus.$emit('自定义事件名',实参)

methods:{

        handleClick(){

        this.bus.$emit('openMenu',true)
       }
}

// 父组建使用:this.bus.$on("自定义事件名", msg => {})

mounted() {

    this.bus.$on("openMenu", msg => {

    this.show = msg;
   });

}

 

猜你喜欢

转载自blog.csdn.net/weixin_39907729/article/details/81625558
今日推荐