如何优雅的引入第三方库其实一直是angular的一个问题,今天就来解答一下:
- 在项目下安装lodash
npm install lodash --save
- 安裝 Lodash Type 定义类型
npm install @types/lodash --save-dev
- 在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"
]
}
}
- 如何使用?
import * as _ from 'lodash';
_.remove(scores, 2); // 正常使用即可