typescript Angular 2:错误 TS2307:找不到模块“socket.io-client”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39226729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 03:48:26  来源:igfitidea点击:

Angular 2: error TS2307: Cannot find module 'socket.io-client'

angulartypescriptsocketstypescript-typings

提问by Jils

After having installed the module socket.io

安装模块后 socket.io

npm install socket.io --save

I have the following error:

我有以下错误:

error TS2307: Cannot find module 'socket.io-client'

错误 TS2307:找不到模块“socket.io-client”

import

进口

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

systemjs.config.js

systemjs.config.js

var map = {
    'socket.io-client': 'node_modules/socket.io-client/socket.io.js'
}

var packages = {
    'socket.io-client': { main: 'socket.io', format: 'cjs', defaultExtension: 'js' }
}

package.json

包.json

"dependencies": {
    "socket.io": "^1.4.8"
}

typings.d.ts

打字.d.ts

/// <reference path="../socket.io-client/socket.io.js" />

declare module 'socket.io-client' {
  var e: any;
  export = e;
}

socket.io-client (Directory)
- socket.io.js
- typings.d.ts

socket.io客户端(目录)
- socket.io.js
- typings.d.ts


Angular 2 RC5


角 2 RC5

回答by j2L4e

Update 2018

2018 年更新

To properly use socket.io in the browser you need to install both the socket.io client package and its typings:

要在浏览器中正确使用 socket.io,您需要安装 socket.io 客户端包及其类型:

npm i socket.io-client @types/socket.io-client 


outdated:

过时:

You are missing typings. Open typings.d.tsand add

你缺少打字。打开typings.d.ts并添加

declare module 'socket.io-client' {
  var e: any;
  export = e;
}

You can also try to install typings for socket.io via npm i @types/socket.io-client. I don't know if there are typings available, though.

您还可以尝试通过npm i @types/socket.io-client. 不过,我不知道是否有可用的类型。

回答by Lucas

Make sure to install the correct package with type definitions for socket.io:

确保使用 socket.io 的类型定义安装正确的包:

npm install @types/socket.io-client --save

This will include the types in the correct folder and means you would need no further action in any other file since angular will pick this up.

这将包括正确文件夹中的类型,这意味着您不需要在任何其他文件中进一步操作,因为 angular 会选择它。

回答by enRaiser

I solved it by adding this at the top of my app.components.ts

我通过在 app.components.ts 的顶部添加它来解决它

  /// <reference path="../../typings/globals/socket.io-client/index.d.ts" />