Tôi muốn làm mới giá trị của thuộc tính trên hành động đẩy, nhưng tôi không biết cách truy cập vào thuộc tính từ một hàm!Cách cập nhật thuộc tính từ chức năng trong Aurelia
export class Datas {
prop1 = "my val";
}
var connection = new WebSocket('ws://localhost:8787', 'json');
connection.onmessage = function (e) {
// prop1 of Datas = e.data;
};
Bất kỳ ý tưởng nào?
Chỉnh sửa: Sau khi tải trang, tôi muốn làm mới dữ liệu khi nhận được thông báo đẩy.
Chỉnh sửa 2: Mã kiểm tra
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>
logs:
"nhận được một thông điệp: Data.information = 4"
"nhà nước của Data.information: 4"
"nhà nước của this.information: undefined"
ah yea, thật đơn giản, phải không? :) Tôi đã cập nhật giải pháp của bạn. Bạn không cần nhập khẩu có thể kết buộc. Ngoài ra, bạn không cần biến myModel nếu bạn sử dụng các chức năng mũi tên. Hãy thử một lần. –
cảm ơn bạn đã sửa chữa. Có nó thực sự đơn giản! – Seb