我一直在Angular CLI 2.2.3中使用Lodash而没有任何问题。但是,在将Angular CLI更新到2.3.1后,我遇到了一些问题。第一个错误是错误:无法找到模块' lodash'在angular-cli?中 我通过这样做解决了这个问题:
npm install lodash --save
npm install @types/lodash --save-dev
并添加到angular-cli.json
"scripts": ["../node_modules/lodash/lodash.js”],
这允许我的angular 2项目编译没有错误。但是,当我转到使用lodash foreach的组件时,我收到以下错误:
EXCEPTION: Error in ./FormDetailComponent class FormDetailComponent - inline template:5:73 caused by: __WEBPACK_IMPORTED_MODULE_10_lodash__.forEach is not a function
ErrorHandler.handleError @ error_handler.js:50
next @ application_ref.js:346
schedulerFn @ async.js:91
SafeSubscriber.__tryOrUnsub @ Subscriber.js:223
SafeSubscriber.next @ Subscriber.js:172
Subscriber._next @ Subscriber.js:125
Subscriber.next @ Subscriber.js:89
Subject.next @ Subject.js:55
EventEmitter.emit @ async.js:77
NgZone.triggerError @ ng_zone.js:329
onHandleError @ ng_zone.js:290
ZoneDelegate.handleError @ zone.js:246
Zone.runTask @ zone.js:154
ZoneTask.invoke @ zone.js:345
error_handler.js:52 ORIGINAL EXCEPTION: __WEBPACK_IMPORTED_MODULE_10_lodash__.forEach is not a function
ErrorHandler.handleError @ error_handler.js:52
next @ application_ref.js:346
schedulerFn @ async.js:91
SafeSubscriber.__tryOrUnsub @ Subscriber.js:223
SafeSubscriber.next @ Subscriber.js:172
Subscriber._next @ Subscriber.js:125
Subscriber.next @ Subscriber.js:89
Subject.next @ Subject.js:55
EventEmitter.emit @ async.js:77
NgZone.triggerError @ ng_zone.js:329
onHandleError @ ng_zone.js:290
ZoneDelegate.handleError @ zone.js:246
Zone.runTask @ zone.js:154
ZoneTask.invoke @ zone.js:345
error_handler.js:55 ORIGINAL STACKTRACE:
ErrorHandler.handleError @ error_handler.js:55
next @ application_ref.js:346
schedulerFn @ async.js:91
SafeSubscriber.__tryOrUnsub @ Subscriber.js:223
SafeSubscriber.next @ Subscriber.js:172
Subscriber._next @ Subscriber.js:125
Subscriber.next @ Subscriber.js:89
Subject.next @ Subject.js:55
EventEmitter.emit @ async.js:77
NgZone.triggerError @ ng_zone.js:329
onHandleError @ ng_zone.js:290
ZoneDelegate.handleError @ zone.js:246
Zone.runTask @ zone.js:154
ZoneTask.invoke @ zone.js:345
error_handler.js:56 TypeError: __WEBPACK_IMPORTED_MODULE_10_lodash__.forEach is not a function
at FormTableComponent.setTotalFlex (form-table.component.ts:92)
at FormTableComponent.ngOnInit (form-table.component.ts:64)
at Wrapper_FormTableComponent.ngDoCheck (wrapper.ngfactory.js:54)
at View_FormDetailComponent3.detectChangesInternal (component.ngfactory.js:126)
at View_FormDetailComponent3.AppView.detectChanges (view.js:425)
at View_FormDetailComponent3.DebugAppView.detectChanges (view.js:620)
at ViewContainer.detectChangesInNestedViews (view_container.js:67)
at View_FormDetailComponent1.detectChangesInternal (component.ngfactory.js:261)
at View_FormDetailComponent1.AppView.detectChanges (view.js:425)
at View_FormDetailComponent1.DebugAppView.detectChanges (view.js:620)
at ViewContainer.detectChangesInNestedViews (view_container.js:67)
at CompiledTemplate.proxyViewClass.View_FormDetailComponent0.detectChangesInternal (component.ngfactory.js:326)
at CompiledTemplate.proxyViewClass.AppView.detectChanges (view.js:425)
at CompiledTemplate.proxyViewClass.DebugAppView.detectChanges (view.js:620)
at CompiledTemplate.proxyViewClass.AppView.internalDetectChanges (view.js:410)
ErrorHandler.handleError @ error_handler.js:56
next @ application_ref.js:346
schedulerFn @ async.js:91
SafeSubscriber.__tryOrUnsub @ Subscriber.js:223
SafeSubscriber.next @ Subscriber.js:172
Subscriber._next @ Subscriber.js:125
Subscriber.next @ Subscriber.js:89
Subject.next @ Subject.js:55
EventEmitter.emit @ async.js:77
NgZone.triggerError @ ng_zone.js:329
onHandleError @ ng_zone.js:290
ZoneDelegate.handleError @ zone.js:246
Zone.runTask @ zone.js:154
ZoneTask.invoke @ zone.js:345
error_handler.js:59 ERROR CONTEXT:
ErrorHandler.handleError @ error_handler.js:59
next @ application_ref.js:346
schedulerFn @ async.js:91
SafeSubscriber.__tryOrUnsub @ Subscriber.js:223
SafeSubscriber.next @ Subscriber.js:172
Subscriber._next @ Subscriber.js:125
Subscriber.next @ Subscriber.js:89
Subject.next @ Subject.js:55
EventEmitter.emit @ async.js:77
NgZone.triggerError @ ng_zone.js:329
onHandleError @ ng_zone.js:290
ZoneDelegate.handleError @ zone.js:246
Zone.runTask @ zone.js:154
ZoneTask.invoke @ zone.js:345
error_handler.js:60 DebugContext {_view: View_FormDetailComponent3, _nodeIndex: 4, _tplRow: 5, _tplCol: 73}
ErrorHandler.handleError @ error_handler.js:60
next @ application_ref.js:346
schedulerFn @ async.js:91
SafeSubscriber.__tryOrUnsub @ Subscriber.js:223
SafeSubscriber.next @ Subscriber.js:172
Subscriber._next @ Subscriber.js:125
Subscriber.next @ Subscriber.js:89
Subject.next @ Subject.js:55
EventEmitter.emit @ async.js:77
NgZone.triggerError @ ng_zone.js:329
onHandleError @ ng_zone.js:290
ZoneDelegate.handleError @ zone.js:246
Zone.runTask @ zone.js:154
ZoneTask.invoke @ zone.js:345
Subscriber.js:227 Uncaught ViewWrappedError {_nativeError: Error: Error in ./FormDetailComponent class FormDetailComponent - inline template:5:73 caused by: __…, originalError: TypeError: __WEBPACK_IMPORTED_MODULE_10_lodash__.forEach is not a function
at FormTableComponent…, context: DebugContext}
我将lodash函数导入到我的组件中,如:
import * as _ from "lodash";
以下是我在代码中使用foreach的方法:
printYArr(): void {
this.totalFlex = [];
// Get y
let yArr: number[] = [];
// Get array of y
_.forEach(this.elements, function(obj: FormElement) {
if (yArr.indexOf(obj.y) < 0) {
yArr.push(obj.y);
}
});
console.log('yarr',yArr);
}
我的包主要依赖性如下。
"@angular/common": "^2.3.1",
"@angular/compiler": "^2.3.1",
"@angular/core": "^2.3.1",
"@angular/flex-layout": "^2.0.0-beta.1",
"@angular/forms": "^2.3.1",
"@angular/http": "^2.3.1",
"@angular/material": "^2.0.0-beta.1",
"@angular/platform-browser": "^2.3.1",
"@angular/platform-browser-dynamic": "^2.3.1",
"@angular/router": "^3.3.1",
"angular2-jwt": "^0.1.28",
"angular2-uuid": "^1.1.0",
"core-js": "^2.4.1",
"lodash": "^4.17.4",
"rxjs": "^5.0.1",
"ts-helpers": "^1.1.1",
"zone.js": "^0.7.4"
遇到此问题的其他人可以帮我解决吗?提前致谢