我尝试更新并显示来自变量updated到EventSource函数的消息:
ngOnInit(): void {
const EventSource: any = window['EventSource'];
const fx = new EventSource('/weather/update/');
fx.onmessage = function (evt) {
this.newMessage = evt.data;
console.log(this.newMessage);
}
}在模板中:
<p>{{newMessage}}</p>收到的消息总是空的,但在控制台中我可以看到新消息。
发布于 2020-10-16 11:22:12
您正在丢失上下文this。尝试使用箭头函数以保留this
like this
\/
fx.onmessage = evt => {
this.newMessage = evt.data;
console.log(this.newMessage);
}https://stackoverflow.com/questions/64382456
复制相似问题