vue.jsvuejs2nuxt.jsvue-events

Nuxt Js Event Fires Twice


I am using Nuxt js SSR for an app that am build, I installed Vue Event plugin but when i emit an event it runs twice at the listener. Created hook runs twice too.

Modules am using: Axios, Auth, Toast

Child Component

methods: {
  initPlaylist(songs) {
    console.log(songs)
  }
},
mounted () {
  this.$events.$on('playAll', data => {
    this.initPlaylist(data.songs) //runs twice
  })
}

Parent Component

method () {
    playAll (songs) {
      this.$events.$emit('playAll', songs)
  }
 }

How can i resolve this issues guys? I need your help.


Solution

  • Maybe you have to call that parent's method on client side only.

    you can write code like this to prevent emit on server side:

    methods: {
      playAll(songs) {
        if (process.server) return
    
        this.$events.$emit('playAll', songs)
      }
    }
    

    or do not call playAll method on server side. (eg: created, mounted...)