"Invalid provider for the NgModule" 使用 AoT 时
"Invalid provider for the NgModule" when using AoT
没有 AoT 一切正常。但是在切换装载机之后,我收到了这个错误并且不知道如何修复它,或者到底是哪里出了问题。
Invalid provider for the NgModule 'AppModule in
/xxx/src/app/app.module.ts' - only instances of Provider and Type are
allowed, got: [LoggerService in
/xxx/src/app/services/logger-service.service.ts, LocalStorageService
in
/xxx/node_modules/angular-2-local-storage/dist/local-storage.service.d.ts,
WindowService in /xxx/src/app/services/window.service.ts, ?null?, ...]
来自AppModule
的相关代码:
import CustomHttp from './services/custom-http.service';
...
@NgModule({
...
providers: [
LocalStorageService,
WindowService,
AuthService,
LoggerService,
CustomHttp,
AuthTokenStore,
AuthService,
SchemaValidator,
AuthInterceptor,
DataService,
ErrorHelper,
FileUpload,
{provide: ErrorHandler, useClass: LoggingErrorHandler},
NonAngularDialogsHelper,
ConfirmationService,
SearchHelper,
FormHelper,
DebugHelper,
PuxApplication,
StoreUtils,
TranslationHelper,
MessagesService,
CustomValidatorsService,
GeolocationService,
SavedSearchesService,
LoggedInfoService,
BeTranslate,
CountryHelper,
SuggestionGenerators,
PrimeNgHelper,
UrlHelper,
DocumentClickService,
NavigationHelper,
BeErrorsInterceptor,
DocumentService,
ScrollHelper,
LinkDialogHelper,
HtmlUtilsService,
RouterHelperService,
StripeService,
VatExamplesService,
ContactInfoHelper,
WizardHelper,
PasswordChangingPagesHelper,
LandingPageHelper,
TrackingEventsHelper,
RfoHelper,
ReactiveFormsHelper,
LiveChatService,
CounterActions
]
...
来自 CustomHttp
的片段:
...
@Injectable()
export default class CustomHttp {
...
constructor(private http: Http,
loggerService: LoggerService) {
this.logger = loggerService.createLogger('CustomHttp');
}
...
编辑 1:按要求添加了整个提供程序数组。
AoT 编译器不是很聪明。事实证明它由于某种原因无法处理默认导入...
不工作:
import CustomHttp from './services/custom-http.service';
工作:
import { CustomHttp } from './services/custom-http.service';
对我来说,我在服务的 index.ts 文件中包含了某些服务的重复导出语句。去掉一个后终于可以用了
(您可以使用 Excel 找出重复项。:-))
来自这个link
.....
export * from './custom-message.service';
export * from './department.service';
export * from './custom-message.service'; // duplicated, remove this
.......
没有 AoT 一切正常。但是在切换装载机之后,我收到了这个错误并且不知道如何修复它,或者到底是哪里出了问题。
Invalid provider for the NgModule 'AppModule in /xxx/src/app/app.module.ts' - only instances of Provider and Type are allowed, got: [LoggerService in /xxx/src/app/services/logger-service.service.ts, LocalStorageService in /xxx/node_modules/angular-2-local-storage/dist/local-storage.service.d.ts, WindowService in /xxx/src/app/services/window.service.ts, ?null?, ...]
来自AppModule
的相关代码:
import CustomHttp from './services/custom-http.service';
...
@NgModule({
...
providers: [
LocalStorageService,
WindowService,
AuthService,
LoggerService,
CustomHttp,
AuthTokenStore,
AuthService,
SchemaValidator,
AuthInterceptor,
DataService,
ErrorHelper,
FileUpload,
{provide: ErrorHandler, useClass: LoggingErrorHandler},
NonAngularDialogsHelper,
ConfirmationService,
SearchHelper,
FormHelper,
DebugHelper,
PuxApplication,
StoreUtils,
TranslationHelper,
MessagesService,
CustomValidatorsService,
GeolocationService,
SavedSearchesService,
LoggedInfoService,
BeTranslate,
CountryHelper,
SuggestionGenerators,
PrimeNgHelper,
UrlHelper,
DocumentClickService,
NavigationHelper,
BeErrorsInterceptor,
DocumentService,
ScrollHelper,
LinkDialogHelper,
HtmlUtilsService,
RouterHelperService,
StripeService,
VatExamplesService,
ContactInfoHelper,
WizardHelper,
PasswordChangingPagesHelper,
LandingPageHelper,
TrackingEventsHelper,
RfoHelper,
ReactiveFormsHelper,
LiveChatService,
CounterActions
]
...
来自 CustomHttp
的片段:
...
@Injectable()
export default class CustomHttp {
...
constructor(private http: Http,
loggerService: LoggerService) {
this.logger = loggerService.createLogger('CustomHttp');
}
...
编辑 1:按要求添加了整个提供程序数组。
AoT 编译器不是很聪明。事实证明它由于某种原因无法处理默认导入...
不工作:
import CustomHttp from './services/custom-http.service';
工作:
import { CustomHttp } from './services/custom-http.service';
对我来说,我在服务的 index.ts 文件中包含了某些服务的重复导出语句。去掉一个后终于可以用了
(您可以使用 Excel 找出重复项。:-))
来自这个link
.....
export * from './custom-message.service';
export * from './department.service';
export * from './custom-message.service'; // duplicated, remove this
.......