我试图通过将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
我仍然不明白为什么人们使用lodash以及ES6已经拥有的所有可能性。
https://www.sitepoint.com/lodash-features-replace-es6/
但是如果你想使用它,尝试以这种方式导入它:
import * as _ from "lodash";
从“lodash”导入* as _;请注意,如果您使用“lodash”中的import _,您将收到以下错误:“模块'lodash'没有默认导出”:
或者只是lodash的一个功能:
import wrapperLodash from 'lodash/wrapperLodash';