【问题标题】:ngx-socket-io not working {} has no property msgngx-socket-io not working {} 没有属性 msg
【发布时间】:2019-07-01 09:33:48
【问题描述】:

尝试只学习 ngx-socket-io 教程,但我不断收到错误 Property msg doesn't exist on type {}。

https://www.npmjs.com/package/ngx-socket-io

我已经正确安装了所有东西,并且在 AppModule.ts 中有我需要的所有东西,我还导入了地图和套接字。

我的 ChatService.ts


constructor(private socket: Socket){}

sendMessage(msg: String){
    this.socket.emit("message", msg);
}

getMessage() {
   return this.socket.fromEvent("message").pipe(map(data => data.msg));
}

【问题讨论】:

    标签: sockets angular7


    【解决方案1】:

    .fromEvent 方法是一个泛型方法,它将采用类型说明符...尝试提供特定类型或将数据定义为any。例如:

    在某处定义:

    export IMyDataType {
        msg: string;
    }
    

    然后调用指定类型的fromEvent:

    getMessage() {
       return this.socket.fromEvent<IMyDataType>("message").pipe(map(data => data.msg));
    }
    

    或者假设数据是any

    getMessage() {
       return this.socket.fromEvent("message").pipe(map((data: any) => data.msg));
    }
    

    【讨论】:

      猜你喜欢
      • 2019-06-23
      • 1970-01-01
      • 2013-10-22
      • 2022-09-25
      • 1970-01-01
      • 2019-05-30
      • 2020-04-28
      • 1970-01-01
      • 2023-01-17
      相关资源
      最近更新 更多