如何从 Aurelia 中的函数更新 属性
How to update property from function in Aurelia
我想在推送操作时刷新 属性 的值,但我不知道如何从函数访问 属性!
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>
日志:
"receiving a message : Data.information = 4"
"state of Data.information : 4"
"state of 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;
};
}
}
我想在推送操作时刷新 属性 的值,但我不知道如何从函数访问 属性!
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>
日志:
"receiving a message : Data.information = 4"
"state of Data.information : 4"
"state of 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;
};
}
}