angular - 在 Angular 4 中订阅 Web 套接字后无法使用来自套接字的消息更新 UI 元素
问题描述
我正在编写 Angular 4 编码。我有一个 Web 套接字,我可以使用 console.log() 记录来自 Web 套接字的消息,但是当我使用该消息更新变量时,同样不会反映在 UI 上。请在下面找到代码。
constructor(private zone: NgZone, private cdRef: ChangeDetectorRef, private DataService1: DataService, private calcSevice: PostService, private datepipe: DatePipe, private elementRef: ElementRef) {
this.ws = new SockJS(this.serverUrl);
this.stompClient = Stomp.over(this.ws);
let that = this;
this.stompClient.connect({}, function (frame) {
that.stompClient.subscribe("/updates/public", (message) => {
//this.zone.run(() => {
alert("got message");
this.refreshErrorMessage = message.body;
console.log("Message from socket is" + this.refreshErrorMessage);
});
//});
});
}
我认为这是 zone 的问题,所以我将上面注释的 ngzone 代码放在两行中,但随后它给了我无法读取未定义错误的属性“运行”。请帮忙
解决方案
推荐阅读
- json - 通过 JSON 在 Draw2D.js 中保存和恢复图表
- android - Flutter:build/app/outputs下的apk/release目录和flutter-apk目录有什么区别?
- java - 在spring boot环境中实现自定义注解
- c# - WebApi .Net Framework 4.8 中的 AWS Cloudwatch 日志
- r - 在填充堆积图中外推 100%
- python - 如何使用函数删除列表中的重复行?
- google-cloud-platform - 具有 1:2 cpu/ram 比率的 GCP 机器
- python-3.x - 以下代码如何创建数据框?
- python - 用python从一个字节中读取位
- linux - Tar Jenkins 作业构建日志并发送到 S3