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

vue中使用Bus

时间:2019-08-17 10:34:21      阅读:84      评论:0      收藏:0      [点我收藏+]

标签:pre   created   UNC   creat   dos   for   操作   清除   事件监听   

//安装: npm install vue-bus

1
. // app.js var eventBus = { install(Vue,options) { Vue.prototype.$bus = new Vue() } }; Vue.use(eventBus); 2.然后在组件中,可以使用$emit, $on, $off 分别来分发、监听、取消监听事件: methods: { todo: function () { this.$bus.$emit(todoSth, params); //params是传递的参数 //... } } 3.监听的组件: // ... created() { this.$bus.$on(todoSth, function(params){ //获取传递的参数并进行操作 //todo something }) }, // 最好在组件销毁前 // 清除事件监听 beforeDestroy () { this.$bus.$off(todoSth); }

 

vue中使用Bus

标签:pre   created   UNC   creat   dos   for   操作   清除   事件监听   

原文地址:https://www.cnblogs.com/huanhuan55/p/11367526.html

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