在我的angular7应用程序中,我使用socket.io-client从节点服务器侦听事件。在这个活动上,我想在页面上显示一些数据。但是,数据并不是在更新侦听事件。下面是我的代码
import { Component, OnInit } from '@angular/core';
import * as io from "socket.io-client";
@Component({
selector: 'app-chat',
templateUrl: './chat.component.html',
styleUrls: ['./chat.component.css']
})
export class ChatComponent implements OnInit {
socket = io("http://localhost:3002");
constructor() { }
test:string;
ngOnInit() {
this.socket.on("adminrecivedMsg", function (data) {
this.test= "hello";
})
}
}chat.component.html
{测试}
发布于 2021-01-16 17:12:54
使用箭头函数,组件的关键字引用与函数关键字丢失。
ngOnInit() {
this.socket.on("adminrecivedMsg", (data)=> {
this.test= "hello";
})
}https://stackoverflow.com/questions/65752053
复制相似问题