probil / vue-socket.io-extended

:v::zap: Socket.io bindings for Vue.js and Vuex (inspired by Vue-Socket.io)
MIT License
628 stars 38 forks source link

this._vm is undefined using Vuex modules #523

Closed Kahss closed 3 years ago

Kahss commented 3 years ago

Hi,

I can't find a way to make an emit from inside a socket action work. Here is my code

const actions = {
    socket_pseudoValid (state, pseudo) {
        state.commit('setValidPseudo', pseudo)
        this._vm.$socket.client.emit('ask_for_opp_list')
    },
]

This code is located is a vuex module. When I reach the emit line, vue devtools tells me that this._vm is undefined. Any idea ?

probil commented 3 years ago

Hey @D-Maurel Could you provide a version of vuex/vue library you are using? Accessing this inside the module is not documented so it might be removed (or have already been removed)

I suggest checking approach (1) described here https://github.com/probil/vue-socket.io-extended/issues/91#issuecomment-397232621 as it will also work in any other places (not just outside vuex)

Kahss commented 3 years ago

Hey @probil Here is the return of npm list

├── vue-socket.io-extended@5.0.0-alpha.3
├── vue@3.0.7
└── vuex@4.0.0

If you have any new approach, I'm interested. In the meantime, i'll try the solutions you've linked, thank you !

EDIT : I've tried putting the socket definition in a separate file, but it works, thank you !

probil commented 3 years ago

Oh, I assume it's probably because of vuex 4 & vue 3. It doesn't use classes under the hood anymore. 🤔

probil commented 3 years ago

@D-Maurel let's close the issue if the solution works for you