我有問題,因爲我切換到角度/通用,並將我的AppModule
拆分爲ServerAppModule
和BrowserAppMobile
,未提供來自第三方模塊的服務。角度/通用:沒有供應商DynamicFormValidationService
在這種情況下,我試圖包含@ng2-dynamic-forms
程序包及其服務,但無論如何安排導入,服務DynamicFormValidationService
都不會提供。
我已經嘗試將DynamicFormsCoreModule.forRoot()
直接導入AppSharedModule
或兩個AppModule
類,但沒有成功。
下面是我如何設置我的模塊結構,拆分進口和服務的跨越CoreModule
和SharedModule
。
ServerAppModule
@NgModule({
bootstrap: [AppComponent],
declarations: [AppComponent],
imports: [
ServerModule,
// As explained here, animations module used document hence we need Noop animations module on server
// https://github.com/angular/angular/issues/14784
NoopAnimationsModule,
AppModuleShared
]
})
export class AppModule { }
BrowserAppModule
@NgModule({
bootstrap: [AppComponent],
declarations: [AppComponent],
imports: [
AppModuleShared,
BrowserAnimationsModule
],
providers: [
{
provide: 'BASE_URL',
useFactory: getBaseUrl
}
]
})
export class AppModule {
}
SharedAppModule
@NgModule({
imports: [
BrowserModule,
// our core modules
CoreModule.forRoot(),
SharedModule,
// ngrx/store modules
StoreModule.forRoot(appReducer),
AppRoutingModule,
StoreRouterConnectingModule,
StoreDevtoolsModule.instrument({maxAge: 50})
],
providers: [],
exports: [
SharedModule
],
declarations: [
HomeComponent
]
})
export class AppModuleShared {}
CoreModule
@NgModule({
imports: [
AuthModule.forRoot(),
Ng2BootstrapModule.forRoot(),
DynamicFormsCoreModule.forRoot()
],
exports: [],
providers: []
})
export class CoreModule {
// forRoot allows to override providers
// https://angular.io/docs/ts/latest/guide/ngmodule.html#!#core-for-root
public static forRoot(): ModuleWithProviders {
return {
ngModule: CoreModule,
providers: [
Title,
UtilityService
]
};
}
constructor(@Optional() @SkipSelf() parentModule: CoreModule) {
if (parentModule) {
throw new Error('CoreModule is already loaded. Import it in the AppModule only');
}
}
}
SharedModule
@NgModule({
imports: [
CommonModule,
FormsModule,
ReactiveFormsModule,
RouterModule,
LayoutModule,
Ng2BootstrapModule,
DynamicFormsCoreModule,
DynamicFormsBootstrapUIModule,
// No need to export as these modules don't expose any components/directive etc'
HttpModule,
HttpClientModule
],
declarations: [
],
exports: [
// Modules
CommonModule,
FormsModule,
ReactiveFormsModule,
RouterModule,
LayoutModule,
Ng2BootstrapModule,
DynamicFormsCoreModule,
DynamicFormsBootstrapUIModule
]
})
export class SharedModule {}
boot.browser.ts
if (module.hot) {
module.hot.accept();
module.hot.dispose(() => {
modulePromise.then(appModule => appModule.destroy());
});
} else {
enableProdMode();
}
const modulePromise = platformBrowserDynamic().bootstrapModule(AppModule);
boot.server.ts
enableProdMode();
export default createServerRenderer(params => {
const providers = [
{ provide: INITIAL_CONFIG, useValue: { document: '<appc-root></appc-root>', url: params.url } },
{ provide: APP_BASE_HREF, useValue: params.baseUrl },
{ provide: 'BASE_URL', useValue: params.origin + params.baseUrl },
];
return platformDynamicServer(providers).bootstrapModule(AppModule).then(moduleRef => {
const appRef: ApplicationRef = moduleRef.injector.get(ApplicationRef);
const state = moduleRef.injector.get(PlatformState);
const zone = moduleRef.injector.get(NgZone);
return new Promise<RenderResult>((resolve, reject) => {
zone.onError.subscribe((errorInfo: any) => reject(errorInfo));
appRef.isStable.first(isStable => isStable).subscribe(() => {
// Because 'onStable' fires before 'onError', we have to delay slightly before
// completing the request in case there's an error to report
setImmediate(() => {
resolve({
html: state.renderToString()
});
moduleRef.destroy();
});
});
});
});
});