[英]vue-socket.io Uncaught TypeError: Cannot read property of undefined
I am trying to call data from my Sockets object in a Vue component.我正在尝试从 Vue 组件中的 Sockets 对象调用数据。
data() {
return {
message: []
}
},
sockets: {
chatMessage: data => {
console.log(data); //-> my data from server
console.log(this); //-> undefined
console.log(this.message); //-> Uncaught TypeError
}
}
I'm successfully emitting and receiving the message event from the server because I'm getting data.我成功地从服务器发出和接收消息事件,因为我正在获取数据。 But I don't know why I can't get the message prop from data.
但是我不知道为什么我无法从数据中获取消息道具。
Here are the docs from the library.这是图书馆的文档。 https://github.com/MetinSeylan/Vue-Socket.io
https://github.com/MetinSeylan/Vue-Socket.io
The answer I found was to be that I was using fat arrow functions and so I wasn't binding this
.我发现的答案是我使用的是粗箭头函数,所以我没有绑定
this
。
All I changed was:我改变的是:
sockets: {
chatMessage: function(data){
//-> returns what I needed
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.