码迷,mamicode.com
首页 > 其他好文 > 详细

Vue bus的使用(兄弟|非父子组件传值)-->可以使用一个空的Vue实例作为中央事件总线new Vue()

时间:2018-07-19 23:27:00      阅读:1136      评论:0      收藏:0      [点我收藏+]

标签:.sh   使用   type   ted   注册   this   enme   color   eth   

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

}

 

Vue bus的使用(兄弟|非父子组件传值)-->可以使用一个空的Vue实例作为中央事件总线new Vue()

标签:.sh   使用   type   ted   注册   this   enme   color   eth   

原文地址:https://www.cnblogs.com/zousaili/p/9338975.html

(1)
(1)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!