侦听组件 b 中总线的自定义事件。但是,在组件 a 中调度事件后,它访问组件 b。组件b的监听函数执行了,但是msg数据函数的监听函数没有更新
请不要说Vuex。
相关代码基于Vue CLi3
这里的代码: 组件 A:
<template>
<div>
Component A
<button @click="sendMsg">pushB</button>
</div>
</template>
<script>
import bus from './bus'
export default {
methods: {
sendMsg() {
bus.$emit('send', 'hello Component B')
this.$router.push('/bbb')
}
}
}
</script>
B组份:
<template>
<div>
<p>component B:{{ msg }}</p>
</div>
</template>
<script type="text/javascript">
import bus from './bus'
export default {
data () {
return {
msg: 'bbb'
}
},
mounted () {
bus.$on('send', data => {
console.log(data)
console.log(this)
this.msg = data
})
}
}
</script>
总线.js
import Vue from 'vue';
export default new Vue()
路由器:
const aaa = () => import('@/components/demo/bus/a')
const bbb = () => import('@/components/demo/bus/b')
export default new Router({
routes: [{
path: '/aaa',
component: aaa
},
{
path: '/bbb',
component: bbb
}]
})
我尝试使用 'watch' 来观察 'msg',但是没有用。
你能帮助我吗?
如果可能,我想深入了解“巴士”
波斯汪
HUH函数
翻翻过去那场雪
随时随地看视频慕课网APP
相关分类