我有三个模块,一个组合模块,一个auth模块和一个recordrequest模块。 recordrequest模块需要使用ValuesService作为单例。我在auth服务中使用root来返回一个带有提供者的模块,并在提供者中声明ValuesService。然后在CompositionModule中导入AuthModule.forRoot()。这应该在DI树中导入ValuesService并且可用于recordsrequestmodule。相反,我看到“没有为ValuesService提供服务器!'
”有人可以检查并查看他们是否可以在代码中发现错误。如果需要,我可以包括其他部分。
作文模块
NgModule({
imports: [
BrowserModule,
BrowserAnimationsModule,
HttpModule,
FormsModule,
MaterialModule.forRoot(),
AuthModule.forRoot(),
HttpInterceptorModule,
RouterModule.forRoot(routes),
PubSubModule.forRoot(),
RecordRequestQueueModule
],
providers:
[
SecurityService,
RoleGaurd
],
declarations: [
AuthComponent,
AppComponent,
NavMenuComponent,
RoutingRedirectComponent,
HomeComponent
],
bootstrap:
[
AppComponent
]
})
export class CompositionModule {
}
记录请求队列模块
@NgModule({
imports: [
AuthModule,
CommonModule,
MaterialModule,
HttpModule,
FormsModule,
RouterModule.forChild(recordRequestQueueRoutes)
],
exports: [
RouterModule
],
declarations: [
RecordRequestingQueueComponent,
RecordRequestingWorkItem,
RecordRequestingQueueSection
],
providers: [
QueueClient
]
})
export class RecordRequestQueueModule {
constructor(valuesService: ValuesService) {
console.log("Record Request Queue loaded.");
}
}
验证模块
@NgModule({
imports: [
CommonModule,
HttpModule,
MaterialModule,
FormsModule
],
declarations: [
LoginComponent,
LogoutComponent],
providers: [
LoginClient
],
exports: [
LogoutComponent,
LoginComponent
]
})
export class AuthModule {
static forRoot(): ModuleWithProviders {
return {
ngModule: AuthModule,
providers: [ValuesService]
};
}
constructor() {
console.log("Auth module loaded");
}
}
ValuesService
import { Injectable } from '@angular/core';
@Injectable()
export class ValuesService {
public username: string;
}