2017-02-12 126 views
0

我想包括“下划线”:“^ 1.8.3”使用当前的Angular 2 CLI ,我得到这个错误找不到模块'下划线'。Angular 2 CLI和Underscore

这是我如何尝试导入它。 import * as'from'underscore';

我上线寻找一个解决方案,其中有许多从去年开始将其包含在system-config.js和typings.json文件中。但是我的项目中没有这些文件。我想他们不再由当前的Angular 2 CLI提供。

是否有导入下划线与新的Angular 2 CLI的解决方案?

回答

1

如果要在TypeScript中使用JS库,则需要在先前版本中安装该库typings定义。

在较新的版本TypeScript使用@types系统。所以现在你不需要手动安装打字,你可以写这个。对于第一次安装

npm install -g [email protected] 

然后

npm install --save underscore 
npm install --save @types/underscore 

欲了解更多您可以在The Future of Declaration Files看到。

+0

此方法适合我!问题关闭。谢谢您的帮助。 –

0

当你想添加到库中的extern项目,则需要遵循以下步骤:

  1. npm install --save jQuerynpm install --save bootstrapnpm install --save underscore

  2. 您需要添加新库的路径你脚本数组中的angular-cli.json文件。

    scripts : [ 
        '../node_modules/bootstrap/dist/js/bootstrap.js', 
        '../node_modules/jquery/dist/jquery.js', 
        '../node_modules/underscore/underscore.js' 
    ] 
    

    3(可选)。 如果您还需要插入css,那么只需在样式数组的angular-cli.json处插入css路径即可。