也可以使用這種方法
Vue全局事件總線(任意組件間通信)
這種比較繁瑣
安裝命令
npm i pubsub-js
全局使用
import PubSub from 'pubsub-js'
Vue.prototype.$pubsub = PubSub
發(fā)送消息
Login.vue文章來源:http://www.zghlxwxcb.cn/news/detail-732227.html
methods: {
login()
{
this.$pubsub.publish('getUserId', '13169463709')
}
}
接收消息
User.vue文章來源地址http://www.zghlxwxcb.cn/news/detail-732227.html
created() {
this.$pubsub.subscribe('getUserId', (funName,user_id) => {
// this.$pubsub.subscribe('getUserId', (_,user_id) => {
//消息名可以使用_占位
console.log(user_id)
})
},
beforeDestroy() {
this.$pubsub.unsubscribe(this.$pubsub)
//銷毀
}
到了這里,關(guān)于Vue 消息訂閱與發(fā)布 (任意組件間通信)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!