Quang
Quang

Reputation: 35

Importing lodash functions globally in Angular 4 app

I'm trying to optimise my Angular 4 application by importing lodash functions globally to the app.module.ts. I tried:

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

However, I can't get the reference to that function in a component. This will throw an error:

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

My dependencies (the ones related to the topic):

"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:

ERROR TypeError: __WEBPACK_IMPORTED_MODULE_0_lodash_fp_wrapperLodash___default.a.some is not a function

Upvotes: 0

Views: 1901

Answers (1)

zelda11
zelda11

Reputation: 423

I still don't understand why people use lodash with all posibilities that ES6 already has.

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

But if you want to use it, try to import it in this way:

import * as _ from "lodash";

import * as _ from "lodash"; Please note that If you use import _ from “lodash”, you will receive following error that “Module ‘lodash’ has no default export”:

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

Or for just one function of lodash:

import wrapperLodash from 'lodash/wrapperLodash';

Upvotes: 1

Related Questions