vue组件传参的方法--bus事件总线

一.在main.js里全局注册$bus事件

Vue.prototype.$bus=new Vue()

二.在需要触发的页面

//bus事件触发
this.$bus.$emit('事件名') //不传递参数
this.$bus.$emit('事件名',val)

三.在事件接收的页面

mounted(){
this.$bus.$on('事件名',()=>{console.log(456)}))
this.$bus.$on('事件名',val=>{this.val=val}))}
//销毁bug事件监听
destroyed(){
  this.$bus.$off('事件名')
 },

注意:如果有时页面组件监听不到事件,可以把销毁时的生命周期换成beforeRouteLeave

beforeRouteLeave() {
    this.$bus.$off("getCurrentTab");
  },

猜你喜欢

转载自blog.csdn.net/weixin_53474595/article/details/129062122