Angular 5:无法找到“ SocketIOClient”

时间:2018-07-06 01:07:19

标签: angular socket.io

我正在尝试在我的有角项目中实现socket.io,但是在编译代码时它无法解析SocketIOClient的名称空间。

我已经安装了@types/socket.io-client,因此angular应用程序会知道societ.io客户端的类型,我不知道为什么当我拥有package.json时,它无法解析它们:

"dependencies": {
    "@angular/animations": "^5.2.0",
    "@angular/common": "^5.2.0",
    "@angular/compiler": "^5.2.0",
    "@angular/core": "^5.2.0",
    "@angular/forms": "^5.2.0",
    "@angular/http": "^5.2.0",
    "@angular/platform-browser": "^5.2.0",
    "@angular/platform-browser-dynamic": "^5.2.0",
    "@angular/router": "^5.2.0",
    "@types/socket.io": "^1.4.36",
    "@types/socket.io-client": "^1.4.32",
    ....
}

当我创建此代码时,这开始发生:

import {Injectable} from "@angular/core";

@Injectable()
export class SocketService {
    socket: SocketIOClient;
}

我正计划提供一项服务来处理应用程序所需的所有套接字操作,我该如何解决?

1 个答案:

答案 0 :(得分:0)

您需要从nodemodules导入套接字io

import * as SocketIOClient from 'socket.io-client';

import {Injectable} from "@angular/core";

@Injectable()
export class SocketService {
    socket: SocketIOClient;
}