utils/bus.js
import Vue from 'vue'
export default new Vue()
import bus from '@/utils/bus'
Vue.prototype.$bus = bus
A页面
export default {
data () {
return {
helloData:'hello'
};
},
methods: {
btnClick () {
this.$nextTick(function () { //加个nextTick解决第一次没数据
this.$bus.$emit('test', this.helloData)
})
},
}
B页面
mounted () {
this.$bus.$on('test', val => {
console.log("接收到bus数据:",val);
this.cdata = val
})
},
destroyed () {
this.$bus.$off() //在页面销毁生命周期加上bus.$off() 解决重复触发的问题
}