在Angular2 如何通过npm安装lodash模块

在Angular2 如何通过npm安装lodash模块

为了更好地在angular2 里面对JSON 数据进行处理。我们可以添加lodash模块来更加简便我们的工作

这里我简单介绍一下如何安装lodash 到angular2

前提:

01 这里默认已经安装了node.js。

02 默认typescript 版本>2.0 eg "typescript": "^2.0.3",

03 默认已经创建angular2 npm 架构的项目

在Angular2 如何通过npm安装lodash模块



1 通过npm 安装lodash.这里可以通过以下命令行来安装

$ npm i -g npm
$ npm i --save lodash
$ npm install --save @types/lodash //this is for typescript

安装完成后node_modules 文件夹多出一个lodash文件夹


2 在system.config.js 文件添加如下

map:{
...
,
'lodash': 'node_modules/lodash/lodash.js',

}


3 创建一个测试component 或者service 来引用这个lodash模块

import * as _ from 'lodash';



4测试lodash是否可以使用。我在ngOnInit()下调用lodash, 可以是否可以遍历数组

ngOnInit(){
            console.log('lodash version:', _.VERSION);
			this.logger.logger("test logger");
			var test = _.each([1,2,3,4],function(v){
				console.log(v);
			});
        }