我想在使用angular-cli 构建的 angular2项目中包含underscore.js。
到目前为止,我无法这样做。到目前为止,我尝试过:
1- npm install下划线--save
2- tsd安装下划线
3-脚本src =“node_modules/underscore/underscore.js”,在index.html中引用
4-内部system-config.js/** Map relative paths to URLs. */
var map = {
'underscore': '../node_modules/underscore/underscore.js'
};
/** User packages configuration. */
var packages = {
'underscore': '../node_modules/underscore/underscore.js'
};
5-从'下划线'中导入*作为_;
但是在运行时,underscore.js不会复制到'dist'目录中,并且浏览器提示找不到underscore.js。我想我在第4点错过了一些东西。
在我开始学习angular2时,任何帮助都将受到赞赏。
请记住,该项目是由angular-cli进行的,而不是其他任何种子项目。除了Underscore.js之外,项目还可以正常运行。
[编辑]
package.json在依赖项中具有“下划线”:“^ 1.8.3”
最佳答案
使用Angular CLI 1.0.0-rc.1,此处介绍了推荐的解决方案:
Angular2 2.4 How to load external libraries sush as Underscore into angular2.
npm install underscore --save // save to dependencies: required to run
npm install @types/underscore --save-dev // save to dev dependencies: required in dev mode
然后在您的组件类中:
import * as _ from 'underscore';
...
subtitle = _.head(['xyz'])
另外,还有另一种方法可以在angular-cli中加载“静态”脚本,如https://www.npmjs.com/package/angular-cli#global-library-installation所述:
在.angular-cli.json中,将其添加到脚本数组:
"scripts": ["../node_modules/underscore/underscore.js"]
这将加载underscore.js,但这不是使其可以在Typescript类中使用的好方法。
关于angular2-cli - 如何在使用angular-cli构建的angular2项目中包含Underscore.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37630127/