this.$root.$emit not working in Vue

后端 未结 2 1467
粉色の甜心
粉色の甜心 2021-01-07 23:23

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-tok         


        
相关标签:
2条回答
  • 2021-01-07 23:38

    I've to use the following approach:

    mounted() {
      this.$nuxt.$emit('event-name')
    } 
    
    mounted() {
      this.$nuxt.$on('event-name', () =>
        ...
      }
    }
    

    Using Nuxt 2.14

    0 讨论(0)
  • 2021-01-07 23:49

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

    Change this:

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

    for this:

    this.$root.$on('access-token', this.setAccessToken);
    
    0 讨论(0)
提交回复
热议问题