this.$root.$emit 在 Vue 中不起作用 [英] this.$root.$emit not working in Vue

查看:40
本文介绍了this.$root.$emit 在 Vue 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在根组件上发出一个事件,并在根组件中监听.在子组件的子组件中,我这样做:

I want to emit an event on the root component, and listen to in in the root component. In a child of child component I do this:

this.$root.$emit('access-token', accessToken);

在根组件(顶部组件,首先加载)我这样做(这是在mounted()方法中):

In root component (the top component, first to load) I do this (edit: this is in the mounted() method):

this.$on('access-token', this.setAccessToken);

它不会对事件做出反应.为什么?

It doesn't react to the event though. Why?

推荐答案

您没有将 $root 用于事件 $on

You are not using the $root for the event $on

改变这个:

this.$on('access-token', this.setAccessToken); 

为此:

this.$root.$on('access-token', this.setAccessToken);

这篇关于this.$root.$emit 在 Vue 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆