Seb*_*Seb 5 javascript ecmascript-6 aurelia
我想在推送操作上刷新属性的值,但我不知道如何从函数访问属性!
export class Datas {
prop1 = "my val";
}
var connection = new WebSocket('ws://localhost:8787', 'json');
connection.onmessage = function (e) {
// prop1 of Datas = e.data;
};
Run Code Online (Sandbox Code Playgroud)
有任何想法吗 ?
编辑:页面加载后,我想在接收推送消息时刷新数据.
编辑2:测试代码
data.js:
export class Data {
static information = '';
}
Run Code Online (Sandbox Code Playgroud)
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);
};
Run Code Online (Sandbox Code Playgroud)
viewModel.html:
<template>
<span id="spanAff">${information}</span>
<br/>
<input type="button" value="log" click.trigger="logState()" />
</template>
Run Code Online (Sandbox Code Playgroud)
日志:
"收到消息:Data.information = 4"
"Data.information的状态:4"
"this.information的状态:undefined"
我在另一个论坛得到了这个解决方案
export class ViewModel {
constructor() {
this.information = 'my val';
var connection = new WebSocket('ws://localhost:8787', 'json');
connection.onmessage = e => {
this.information = e.data;
};
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1199 次 |
| 最近记录: |