首页 文章

使用Angular CLI的第三方库类型

提问于
浏览
3

我试图弄清楚如何在Angular CLI环境中使用各种第三方库 . 官方的wiki page对于使用moment.js lib非常有用 . 但是,在尝试为库安装打字时遇到了麻烦(本教程中的步骤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

虽然错误消息建议"uninstall the copy installed by Typings",但似乎没有新的 .d.ts 文件添加到项目文件夹中 . 在这种情况下,包含第三方打字的最佳方式是什么?

1 回答

  • 0

    简答:运行 npm install @types/moment .

    说明:

    你这里至少有几个问题 .

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

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

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

相关问题