小编Dak*_*ksh的帖子

在 socket.io-client 中调用 IO 时出错,角度为 8

我在我的 angular 项目中调用 socket.io-client 的 IO,如下所示:

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/internal/Observable';
import * as io from 'socket.io-client';
@Injectable({
  providedIn: 'root'
})
export class SocketclientService {
  socket:any;
  url:any="ws://localhost:8080"
  constructor() {
    this.socket = io(this.url);
   }
   
   listen(eventname:any,data:any){
     return new Observable((subscriber)=>{
       this.socket.on(eventname,(data:any)=>{
         subscriber.next(data);
       })
     })
   }
   emit(eventname:string,data:any){
     this.socket.emit(eventname,data);
   } 
}
Run Code Online (Sandbox Code Playgroud)

但我总是收到这样的错误:

 Error: src/app/services/socketclient.service.ts:11:19 - error TS2349: This expression is not callable.
      Type 'typeof import("C:/somepathtoproject/node_modules/socket.io-client/build/index")' has no call signatures.
    
    11     this.socket = io(this.url);
                         ~~
Run Code Online (Sandbox Code Playgroud)

我不明白为什么会这样显示,即使我已经安装了 @types/socket.io-client

socket.io angular

3
推荐指数
3
解决办法
8793
查看次数

标签 统计

angular ×1

socket.io ×1