【发布时间】:2016-08-04 01:04:03
【问题描述】:
我用两种方法实现了lodash:
方法一:
我在 bower_component 中安装并在组件中创建 declare var _:any;。它工作正常
在点击事件中我调用了这个函数:
checkLodash() {
_.forEach({ a: 1, b: 2 }, function (value, key) {
console.log(key);
});
let a = _.findIndex(this.users, 'active');
console.log(a);
}
一切正常。但我在生产中遇到了这个问题
EXCEPTION: ReferenceError: _ is not defined
我从这里得到了一些解决方案 [https://stackoverflow.com/questions/36171727/lodash-in-angular2-declare-var-any-not-working][link]
方法二:
在我的应用程序中没有 systemjs,因为我使用的是 [seed-app][1],所以我使用了tsd install lodash --save
和
import * as _ from 'lodash';
我在点击事件中调用这个函数
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);
}
现在我的输出是:
一种
b
(在 console.log 中)
如果我单击按钮调用checkLodash 函数,我会收到此错误,
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)
如何解决这个问题?
【问题讨论】: