Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/422.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Websocket onmessage事件不更新组件数据_Javascript_Callback_Event Handling_Vue.js - Fatal编程技术网

Javascript Websocket onmessage事件不更新组件数据

Javascript Websocket onmessage事件不更新组件数据,javascript,callback,event-handling,vue.js,Javascript,Callback,Event Handling,Vue.js,我正在将我的回调设置为: data() { return { code: 'Apple', } }, mounted() { console.log(this.code) // prints 'Apple' this.$options.sockets.onopen = this.wsOpen(); this.$options.sockets.onmessage = this.logMessage(msg); }, methods: {

我正在将我的回调设置为:

data() {
    return {
        code: 'Apple',
    }
},
mounted() {
    console.log(this.code) // prints 'Apple'
    this.$options.sockets.onopen = this.wsOpen();
    this.$options.sockets.onmessage = this.logMessage(msg);
},
methods: {
    logMessage(msg){
        this.code += "\n" + msg.data;
        console.log("this.code: " + this.code);
    },
}
然而,它告诉我“msg”没有定义。 但是,以下工作不在本规范的范围内:

this.$options.sockets.onmessage= function (msg) {
        console.log(msg.data) // this works, msg is not undefined here
        console.log(this.code) // doesnt work, this.code is undefined
    }

我想我在做一些愚蠢的事情。

把它设置为函数

this.$options.sockets.onmessage = this.logMessage;
代码当前正在将
onmessage
设置为
此.logMessage(msg)
的结果,并且作为错误状态,未定义
msg