Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 调用socket.IO-client中的IO时出错,第8页_Angular_Socket.io - Fatal编程技术网

Angular 调用socket.IO-client中的IO时出错,第8页

Angular 调用socket.IO-client中的IO时出错,第8页,angular,socket.io,Angular,Socket.io,我正在我的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 SocketclientServi

我正在我的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

您的导入语句不正确,它应该是这样的

import io from 'socket.io-client';
如果你仔细观察,你会发现它被声明为

declare module 'socket.io-client' {
 export = io;
}
这意味着

export=语法指定从模块导出的单个对象


如果我理解正确,这与上述导入语法的要求类似,即使我在过去两天也面临同样的问题,下面是我如何解决这个问题的:

->从“socket.io客户端”导入*作为io//在较新版本中出现错误,不确定原因

所以使用->从“socket.io客户端”导入{io}//它不会抛出任何错误

1) 首先,错误是由于socket.io客户端的较新版本(即版本>3.0-几天前发布)。所以试着使用最稳定的(我在客户端和服务器端都使用了2.3.0)
2) 确保您在客户端使用的套接字版本与服务器中使用的套接字版本相匹配。

我找到了解决方法,我稍微研究了一下socket.io-client模块,并在“index.d.ts”下找到了以下行:

export { lookup as io, Socket, SocketOptions };
所以我试着这样导入:

import {io} from 'socket.io-client/build/index';
然后使用它:

this.socket=io(url);

它对我很有用。

为上述示例安装以下版本


$npm i socket.io-client@2.2.0--在angular 10中保存

效果良好:

import {io} from 'socket.io-client';
这是官方文件

以下是2021年更新的使用Typescript执行此操作的方法:


从“Socket.io客户端”导入{io,Socket};
导出默认类SocketService{
插座:插座;
建造商(康涅狄格州){
this.socket=io(连接);
(...)
}
}

应在括号内导入
io
,并且还应将套接字的
socket
类型也从
'socket.io client'
导入,这可能是由单独安装的类型定义引起的。但是,它们现在与
socket.io客户机
包一起提供()

因此,只需卸载它们并更改导入:

npm卸载@types/socket.io客户端

从“Socket.io客户端”导入{io,Socket};

实际上,您的代码是正确的,但我相信您使用的是更新版本的socketIO(3.0)客户端

使用以下版本: 将条目粘贴到package.json中并运行npmi

**"socket.io-client": "2.2.0"**,
或者只需删除早期版本并安装2.0版本

$npm i socket.io-client@2.2.0 --save

我正在使用const io=require('socket.io client)导入{io}。。。正在解决此问题“模块”“socket.io客户端”“没有导出的成员”“io”“.ts(2305)”好的,只有在安装了@types/socket.io-clientVSCode的情况下,typescript上才会出现此问题,并发出以下警告:
Module”“socket.io客户端”“没有导出的成员”“io”“.ts(2305)
但一切正常。这应该是可以接受的答案。工作正常,他还必须将套接字的类型更改为socket:socket,它是从同一依赖项导入的。@viniciusaruda和其他:这是由手动安装的类型定义引起的。但它们现在包含在npm包中。所以只需删除它们:
npm uninstall@types/socket.io client