使用Angular CLI

时间:2016-06-21 09:24:03

标签: typescript angular angular-cli

我试图弄清楚如何在Angular CLI环境中使用各种第三方库。官方wiki page对于使用moment.js lib非常有用。但是,在尝试为库安装 typings 时遇到了麻烦(本教程中的步骤1a):

typings install moment --save

typings WARN hastypings Typings for "moment" already exist in 
"node_modules/moment/moment.d.ts". You should let TypeScript resolve
the packaged typings and uninstall the copy installed by Typings

typings ERR! message Unable to find "moment" ("npm") in the registry.
Did you want to try searching another source? Also, if you want contribute
these typings, please help us: https://github.com/typings/registry

我的参考设置:

angular-cli: 1.0.0-beta.5
node: 4.4.4
os: darwin x64
typings: 1.0.5

虽然错误消息建议“卸载由Typings安装的副本”,但似乎没有新的.d.ts文件添加到项目文件夹中。在这种情况下,包含第三方类型的最佳方式是什么?

1 个答案:

答案 0 :(得分:0)

简答:运行npm install @types/moment

说明:

这里至少有几个问题。

(1)作为TypeScript团队announced,“在TypeScript 2.0中获取类型声明将需要除了npm之外没有工具”(强调他们的)。这意味着安装输入文件需要typings has been deprecated和npm 单独。一个简单的例子是npm install --save @types/lodash。记下@types。所有类型现在都在node_modules/@types。考虑到这一点,我们可以看看你的第二个问题。

(2)您正在使用typings,一个TypeScript 1工具,angular-cli,它使用TypeScript 2.它们不兼容。相反,您可以使用momentjs安装npm install @types/moment的打字。

最后 - 与您的问题没有直接关系 - 您可能对angular2-moment感兴趣,这是一个将momentjs移植到Angular 2的开源库。