我想在推送操作上刷新属性的值,但我不知道如何从函数访问该属性!
export class Datas {
prop1 = "my val";
}
var connection = new WebSocket('ws://localhost:8787', 'json');
connection.onmessage = function (e) {
// prop1 of Datas = e.data;
};
有什么想法吗?
编辑:页面加载后,我想在收到推送消息时刷新数据。
编辑2:测试代码
data.js:
export class Data {
static information = '';
}
viewModel.js
import {bindable} from 'aurelia-framework';
import { Data } from 'data';
export class ViewModel {
constructor() {
this.informaton = Data.information;
}
logState(){
console.log("state of Data.information : " + Data.information);
console.log("state of this.information : " + this.information);
}
}
var connection = new WebSocket('ws://localhost:8787', 'json');
connection.onmessage = function (e) {
Data.information = e.data;
console.log("receiving a message : Data.information = " + Data.information);
};
viewModel.html:
<template>
<span id="spanAff">${information}</span>
<br/>
<input type="button" value="log" click.trigger="logState()" />
</template>
日志:
“收到消息:Data.information = 4”
“数据状态:4” “this of this.information:undefined”
答案 0 :(得分:7)
我在另一个论坛上得到了这个解决方案:
export class ViewModel {
constructor() {
this.information = 'my val';
var connection = new WebSocket('ws://localhost:8787', 'json');
connection.onmessage = e => {
this.information = e.data;
};
}
}