probil / vue-socket.io-extended

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

this._vm in vuex error #449

Open gutisalex opened 4 years ago

gutisalex commented 4 years ago

Using vuex-class-component with typescript I get an error trying to dispatch an action and commiting an event the way it is described in the docs:

Code:

this._vm.$socket.client.emit('SomeEvent', data);

Error:

any Property '_vm' does not exist on type 'MyModule'.ts(2339)

Any idea?

probil commented 4 years ago

Hey, @gutisalex Could you please show me entire action? TypeScript doesn't always transpile actions the way you expect.

I suppose you use arrow function which doesn't have this

jaqusa commented 4 years ago

I had the same problem using class components in nuxt.

This happens when you use this because referencing the class and not the entire document. The solution that worked for me was to directly access the prototype of vue

you should have something like this

Vue.prototype.$socket.client.emit ('Some Data', data);