[Solved] Accessing this.$socket into a Vuex action.js file ?



  • Hi,
    I’m using vue-socket.io-extend to manage websockets.
    I would like to trigger a socket.emit() from within a Vuex actions.js file but this throws an error:
    TypeError: Cannot read property 'emit' of undefined

    Should I have to import anythig?

    Here is my action.js file

    export const saveUserLog = ({ commit, rootState }, data) => {
      data.user = rootState.globalModule.userConnected._id
      commit('addUserLogToTempMemory', data)
      this.$socket.emit('saveUserLog', data) // throws an error
    }
    
    Thanks for help !


  • I finally found the solution by myself in this topic

    import Vue from 'vue' // importing Vue
    
    export const saveUserLog = ({ commit, rootState }, data) => {
      data.user = rootState.globalModule.userConnected._id
      commit('addUserLogToTempMemory', data)
      Vue.$socket.emit('saveUserLog', data) // Using Vue
    }