angular6引入lodash

如何优雅的引入第三方库其实一直是angular的一个问题,今天就来解答一下:

  1. 在项目下安装lodash
npm install lodash --save
  1. 安裝 Lodash Type 定义类型
npm install @types/lodash --save-dev
  1. 在tsconfig.json添加lodash类型( "types" : ["lodash"])
tsconfig.json
{
 "compileOnSave": false,
 "compilerOptions": {
  "outDir": "./dist/out-tsc",
  "sourceMap": true,
  "declaration": false,
  "moduleResolution": "node",
  "emitDecoratorMetadata": true,
  "experimentalDecorators": true,
  "target": "es5",
  "typeRoots": [
   "node_modules/@types"
  ],
  "types" : ["lodash"],
  "lib": [
   "es2017",
   "dom"
  ]
 }
}

  1. 如何使用?
import * as _ from 'lodash';


 _.remove(scores, 2); // 正常使用即可

转载于:https://my.oschina.net/u/3615170/blog/1833451

你可能感兴趣的:(angular6引入lodash)