4如何将下划线作为Underscore加载到angular2中

4如何将下划线作为Underscore加载到angular2中

本文介绍了Angular2 2.4如何将下划线作为Underscore加载到angular2中.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用angular-cli创建了一个应用程序,我需要导入下划线之类的外部库.我是angular 2.4的新手,以前没有使用过SystemJS或Webpack.有人可以逐步指导我如何将下划线加载到我的angular 2.4项目中.

I created an app with angular-cli and I need to import external libraries such as underscore. I'm new to angular 2.4 and haven't used SystemJS nor Webpack before. Can someone give me a step by step guid of how to load underscore into my angular 2.4 project.

使用Angular-cli最新版本"创建的带有下划线的项目的github链接会让我非常高兴.读代码很好;)

A link to github with a project created with angular-cli "latest version" with underscore would make me super happy. Reading code is nice ;)

----以下只是描述让我感到困惑的原因------

---- Following is just to describe what makes me confused ------

从我的研究中,我发现了两种加载模块的替代方法.

From my research I found 2 alternatives to load modules.

  1. SystemJS -大多数记录在angular.io
  2. Webpack -使用的是angular-cli.
  1. SystemJS - Most documented in angular.io
  2. Webpack - Is what angular-cli is using.

哪个最好用?

//package.json "name": "angular", "version": "0.0.0", "license": "MIT", "angular-cli": {}, "scripts": { "ng": "ng", "start": "ng serve", "test": "ng test", "pree2e": "webdriver-manager update --standalone false --gecko false", "e2e": "protractor" }

//package.json "name": "angular", "version": "0.0.0", "license": "MIT", "angular-cli": {}, "scripts": { "ng": "ng", "start": "ng serve", "test": "ng test", "pree2e": "webdriver-manager update --standalone false --gecko false", "e2e": "protractor" }

cli在脚本标签中创建对"ng serve"的引用.我应该删除该行并将其替换为webpack吗?

The cli creates a reference to "ng serve" in the script tag. Should I remove that line and replace it with webpack?

...如果是这样.我是否必须设置所有已经完成的角度设置加上我的设置,还是只在顶部添加我的设置吗?

... If so. Do I have to set up all the settings angular already done plus mine or is it an easier way you just add my settings on top?

推荐答案

使用以下命令:对于Angular CLI

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';

let rs = _.map([1, 2, 3], function(num){ return num * 3; });
console.log(rs);

这篇关于Angular2 2.4如何将下划线作为Underscore加载到angular2中.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 14:08