vue组件传值 part2

非父子组件传值

轻量级 视图层框架

复杂组件间传值,引进其他的工具或者设计模式

1.vuex

2.总线机制

//main line

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

Vue.prototype.bus=new Vue();

2.在组建中使用

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

methods:{
        handleClicks(){
        this.bus.$emit('openMenu',true)
       }
}

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

mounted() {
    this.bus.$on("openMenu", msg => {
    this.show = msg;
   });

}

 

posted @ 2019-11-27 18:45  容忍君  阅读(205)  评论(0)    收藏  举报