2017-08-02 4 views
0

私はマウントで、私のコールバックを設定しています:のWebSocketのonMessageイベントdoesntの更新コンポーネントデータ

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.codeは適用範囲外となり:

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 
    } 

私はイムはダム何かを考えます。

答えて

1

機能に設定するだけです。

this.$options.sockets.onmessage = this.logMessage; 

コードは、現在結果this.logMessage(msg)onmessage設定され、エラー状態として、msgmountedに定義されていません。

関連する問題