I have implemented lodash in two methods:
Method 1:
I have Install in bower_component and make a declare var _:any;
in components. Its working fine
In click event i am calling this function:
checkLodash() {
_.forEach({ a: 1, b: 2 }, function (value, key) {
console.log(key);
});
let a = _.findIndex(this.users, 'active');
console.log(a);
}
Everything works fine. but I got this issue in production
EXCEPTION: ReferenceError: _ is not defined
I got some solution from here [https://stackoverflow.com/questions/36171727/lodash-in-angular2-declare-var-any-not-working][link]
Method 2:
In my app there is no systemjs because i am using [seed-app][1], so i have used
tsd install lodash --save
and
import * as _ from 'lodash';
I am calling this function in click event
constructor() {
this.show_data = 'hello';
this.users = [
{ user: 'barney', active: false },
{ user: 'fred', active: false },
{ user: 'pebbles', active: true },
];
_.forEach({ a: 1, b: 2 }, function (value, key) {
console.log(key);
});
}
checkLodash() {
_.forEach({ a: 1, b: 2 }, function (value, key) {
console.log(key);
});
let a = _.findIndex(this.users, 'active');
console.log(a);
}
Now my output is :
a
b
(in console.log)
If I click button to call checkLodash
function, I am getting this error,
EXCEPTION: Error during evaluation of "click"
ORIGINAL EXCEPTION: TypeError: _.findIndex is not a function
browser_adapter.js:76 TypeError: _.findIndex is not a function
at LodashWithoutDeclare.checkLodash (lodash-without-declare.ts:31)
at AbstractChangeDetector.ChangeDetector_LodashWithoutDeclare_0.handleEventInternal (viewFactory_LodashWithoutDeclare:75)
at AbstractChangeDetector.handleEvent (abstract_change_detector.js:57)
at AppView.triggerEventHandlers (view.js:221)
at eval (viewFactory_LodashWithoutDeclare:130)
at dom_renderer.js:282
at dom_events.js:28
at ZoneDelegate.invoke (angular2-polyfills.js:332)
at Object.NgZoneImpl.inner.inner.fork.onInvoke (ng_zone_impl.js:44)
at ZoneDelegate.invoke (angular2-polyfills.js:331)
How to fix this issue?