Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
312 views
in Technique[技术] by (71.8m points)

angular - Angular2 2.4 How to load external libraries sush as Underscore into angular2.

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.

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 - Most documented in angular.io
  2. Webpack - Is what angular-cli is using.

Which one is best to use?

//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" }

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

using following commands: For 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

in Component:

import * as _ from 'underscore';

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...