gpt4 book ai didi

javascript - 如何从 Aurelia 中的函数更新属性

转载 作者:数据小太阳 更新时间:2023-10-29 06:06:11 26 4
gpt4 key购买 nike

我想在推送操作时刷新属性值,但我不知道如何从函数访问该属性!

export class Datas {
prop1 = "my val";
}

var connection = new WebSocket('ws://localhost:8787', 'json');
connection.onmessage = function (e) {
// prop1 of Datas = e.data;
};

有什么想法吗?

编辑:页面加载后,我想在收到推送消息时刷新数据。

编辑 2:测试代码

数据.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;
};
}
}

关于javascript - 如何从 Aurelia 中的函数更新属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30057559/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com