throws ERROR TypeError: Cannot read property 'emit' of undefined, has deprecated this is happening in my chat socket application

throws ERROR TypeError: Cannot read property 'emit' of undefined, has deprecated this is happening in my chat socket application

这是我遇到错误的服务文件代码

 public sendMessage(msg: SocketMessage) {
this.socket.emit('check-message', msg);

}

checkNewMessages(): Observable<SocketMessage> {
    return Observable.create((observer) => {
      if (environment.production)
        this.socket.on('check-message', (message) => {
          observer.next(message);
        });

    })
  }

您的套接字 属性 未定义。你如何申报?

也许您必须在服务构造函数中声明。