我在我的 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);
}
}
但我总是收到这样的错误:
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);
~~
我不明白为什么会这样,即使我已经安装了 @types/socket.io-client
在角度 10 中效果很好:
import {io} from 'socket.io-client';
这是官方文档https://socket.io/docs/v3/client-api/index.html https://socket.io/docs/v3/client-api/index.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)