vue-router 路由组件 和 主APP通信问题

现有vue-router的一个webapp,有一个聊天页面进入后连接socket,切换到其他路由后,监听客户发过来的消息,在页面上面提示用户。我在聊天路由中使用this.$dispatch('admin_msg_notify'),在主路由中使用

let App = Vue.extend({
    events: {
        //消息通知
        admin_msg_notify : 'showMsgToast'
    },
    methods : {
        showMsgToast(){
            console.log('主屏幕来消息了');
            $.toast('消息来了')
        }
    }
});

router.start(App,'#app');

监听不到消息通知?

阅读 6.6k
1 个回答

路由组件的$parent 是null 所以获取不到
使用vue-router的属性

this.$router.app.$emit('admin_msg_notify');
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题