在Angular 4 app中全局导入lodash函数

问题描述 投票:-1回答:1

我试图通过将lodash函数全局导入app.module.ts来优化我的Angular 4应用程序。我试过了:

import { some } from 'lodash/fp/some';
import _ from 'lodash/wrapperLodash';

但是,我无法在组件中获得对该函数的引用。这会抛出一个错误:

_.some(myVar, { lat: null });

我的依赖项(与主题相关的依赖项):

"devDependencies": {
"@angular/compiler": "^4.2.4",
"@angular/core": "^4.2.4",
"@types/lodash": "^4.14.104",
"lodash": "^4.17.5",
"typescript": "~2.3.3"
}

错误:

ERROR TypeError: __WEBPACK_IMPORTED_MODULE_0_lodash_fp_wrapperLodash___default.a.some is not a function
javascript angular lodash
1个回答
1
投票

我仍然不明白为什么人们使用lodash以及ES6已经拥有的所有可能性。

https://www.sitepoint.com/lodash-features-replace-es6/

但是如果你想使用它,尝试以这种方式导入它:

import * as _ from "lodash";

从“lodash”导入* as _;请注意,如果您使用“lodash”中的import _,您将收到以下错误:“模块'lodash'没有默认导出”:

来自https://hassantariqblog.wordpress.com/2016/10/15/angular2-import-lodash-into-angular2-application-using-typescript/

或者只是lodash的一个功能:

import wrapperLodash from 'lodash/wrapperLodash';
© www.soinside.com 2019 - 2024. All rights reserved.