-
-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot read property 'emit' of undefined #61
Comments
It works if I put it inside new Vue({
sockets: {
connect: () => {
this.$socket.emit("join_room", "abc");
},
},
render: h => h(App),
}).$mount('#app'); |
Hi, @gijo-varghese Thanks for your question. Could you try to use function instead of arrow function? sockets: {
connect() {
this.$socket.emit("join_room", "abc");
},
}, |
Cool. That worked, thanks! It would be great if you can mention that in the docs |
Yeah, great idea 👍 . Added to backlog |
@probil similarly, how can I emit from a store action? SOCKET_CONNECT: (state) => {
state.connected = true;
this.$socket.emit('join_room', 'abc'); // Cannot read property 'emit' of undefined
}, |
It seems like you mean mutation, because only mutation accepts state as a first argument. SOCKET_CONNECT(state) {
state.connected = true;
this._vm.$socket.emit('join_room', 'abc'); // <-- `_vm` is required here
}, I know this is not the perfect solution but it should work. Btw, try to move this to action instead. Because mutations are like sync transaction and should only change the state As I know there is now way to extend action context so probably I will implement it using custom namespaced module in store. So socket emit will look like this dispatch('socket/emit', { msg: 'Hi there' }); |
oh ok. For now, I going with |
Vuex is not really big, it's around |
I've a
.vue
file like this:and I'm getting an error:
Uncaught TypeError: Cannot read property 'emit' of undefined
main.js
The text was updated successfully, but these errors were encountered: