简体   繁体   English

vue-socket.io 未捕获的类型错误:无法读取未定义的属性

[英]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.

相关问题 未捕获的类型错误:无法调用 class 作为 function vue-socket.io - Uncaught TypeError: Cannot call a class as a function vue-socket.io Socket.io 未捕获类型错误:无法读取未定义的属性“应用” - Socket.io Uncaught TypeError: Cannot read property 'apply' of undefined Vue:未捕获(承诺)TypeError:无法读取未定义的属性'_c' - Vue: Uncaught (in promise) TypeError: Cannot read property '_c' of undefined VUE:未捕获的类型错误:无法读取未定义的属性“长度” - VUE: Uncaught TypeError: Cannot read property 'length' of undefined 未捕获的类型错误:无法读取未定义的 Vue 和 Leaflet 的属性“removeLayer” - Uncaught TypeError: Cannot read property 'removeLayer' of undefined Vue and Leaflet 未捕获的类型错误:无法读取未定义 vue 的属性“$bvModal” - Uncaught TypeError: Cannot read property '$bvModal' of undefined vue 未捕获的TypeError:无法读取未定义的Phaser.io的属性“ Tween” - Uncaught TypeError: Cannot read property 'Tween' of undefined Phaser.io Socket.io TypeError:无法读取未定义的属性“hteam” - Socket.io TypeError: Cannot read property 'hteam' of undefined Socket.io - UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'emit' of undefined - Socket.io - UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'emit' of undefined Socket.IO TypeError:无法读取未定义的属性“发出” - Socket.IO TypeError: Cannot read property 'emit' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM