watch与mounted写在methed方法之外
其中collpase为被监视的参数 如果collpase改变了将执行这个方法
watch:{
collpase(){
// 传递数据参数 //watch 为监听事件
this.$bus.$emit("H_Sider",this.collpase)
console.log("触发事件了")
}
},
mounted() {
this.$bus.$on('H_Sider',data=>{//on是绑定事件,H_Sider是事件名
console.log("收到数据",data)
this.collapse = data
})
}
}
在main.js中进行配置
beforeCreate() {
Vue.prototype.$bus = this //定义全局总线
}