我是Typescript和Angular 2的新手。我需要安装一个npm依赖项并在我的angular 2应用程序中使用它。
依赖关系为https://www.npmjs.com/package/ng2-stomp-service
我已经安装了必要的软件包,但是我需要将以下内容添加到我的typings.d.ts
文件
declare module 'stompjs';
declare module 'sockjs-client';
我无法在项目中找到typings.d.ts
文件。
到目前为止,我已尝试过以下内容,
npm install typings --global
npm install @types/stompjs
npm isntall @types/sockjs-client
typings install dt~stompjs --save
typings install dt~sockjs-client --save
我有typings.json
个文件包含内容,
{
"dependencies": {
"sockjs-client": "registry:dt/sockjs-client#1.0.3+20160727010356",
"stompjs": "registry:dt/stompjs#2.3.0+20161111105645"
}
}
当我使用npm start
运行我的角度2应用时,它会抛出错误,如下所示
ERROR in D:/Userfiles/subramanians/projects/hand-cricket/node_modules/ng2-stomp-service/dist/stomp.service.ts (27,2): Member 'config' implicitly has an 'any' type.
ERROR in D:/Userfiles/subramanians/projects/hand-cricket/node_modules/ng2-stomp-service/dist/stomp.service.ts (36,2): Member 'queuePromises' implicitly has an 'any[]' type.
ERROR in D:/Userfiles/subramanians/projects/hand-cricket/node_modules/ng2-stomp-service/dist/stomp.service.ts (83,32): Parameter 'str' implicitly has an 'any' type.
ERROR in D:/Userfiles/subramanians/projects/hand-cricket/node_modules/ng2-stomp-service/dist/stomp.service.ts (132,53): Parameter 'response' implicitly has an 'any' type.
ERROR in D:/Userfiles/subramanians/projects/hand-cricket/node_modules/ng2-stomp-service/dist/stomp.service.ts (27,2): Member 'config' implicitly has an 'any' type.
ERROR in D:/Userfiles/subramanians/projects/hand-cricket/node_modules/ng2-stomp-service/dist/stomp.service.ts (36,2): Member 'queuePromises' implicitly has an 'any[]' type.
ERROR in D:/Userfiles/subramanians/projects/hand-cricket/node_modules/ng2-stomp-service/dist/stomp.service.ts (83,32): Parameter 'str' implicitly has an 'any' type.
ERROR in D:/Userfiles/subramanians/projects/hand-cricket/node_modules/ng2-stomp-service/dist/stomp.service.ts (132,53): Parameter 'response' implicitly has an 'any' type.
我不确定是什么导致了这个问题,我猜这是因为我没有在typings.d.ts
请指教。谢谢。
答案 0 :(得分:8)
我对这次谈话的两分钱:
在你的tsconfig.json文件中,确保你的typings.d.ts文件有正确的路径
“typeRoots”:[ “node_modules / @类型”, “../src/typings.d.ts” ],
答案 1 :(得分:2)
如果您没有使用新版本的角色,则无法在项目中找到typings.d.ts
文件。我的建议是使用角度CLI https://cli.angular.io/
答案 2 :(得分:1)
看起来你的Typescript编译器试图从node_modules
目录编译文件。
请确保在tsconfig.json
文件中包含此排除规则:
{
"compilerOptions": {},
"exclude": [
"node_modules"
]
}
顺便说一下。您只需@types
和typings
中的一个。第二个是不推荐使用的,您只需要包含在您的dev依赖项中的正确@types
个包。希望它有所帮助!