My angular application run smoothly most of the times but it sometime fail at startup with messages like
Can't bind to 'ngIf' since it isn't a known property of 'ng-container'.
As already stated at Can't bind to 'ngIf' since it isn't a known property of 'div' the issue is that the CommonModule
is not imported. I'm probably doing something wrong but it seems the CommonModule
is imported (if not it would never work, else it's working 90% of the time).
Here is my configuration:
app.module.ts
@NgModule({
declarations: [AppComponent],
imports: [
CommonModule,
AppRoutingModule,
NavigationModule,
HttpClientModule,
BrowserModule,
BrowserAnimationsModule,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: HttpLoaderFactory,
deps: [HttpClient]
}
}),
AngularSvgIconModule.forRoot()
],
providers: [
{
// tslint:disable-next-line: max-line-length
// Inspired by https://www.mokkapps.de/blog/how-to-build-an-angular-app-once-and-deploy-it-to-multiple-environments/#solution-3-mount-configuration-files-from-environment
// Load configuration file.
provide: APP_INITIALIZER,
useFactory: (envConfigService: EnvironmentService) => async () => envConfigService.loadConfiguration(),
deps: [EnvironmentService],
multi: true
},
CookieService,
{
provide: HTTP_INTERCEPTORS,
useClass: CacheInterceptor,
multi: true
},
{
provide: HTTP_INTERCEPTORS,
useClass: MyHttpInterceptor,
multi: true
},
I18n,
Xliff,
{ provide: TRANSLATIONS, useValue: translationsEn },
{ provide: TRANSLATIONS_FORMAT, useValue: 'xlf' },
{ provide: MatPaginatorIntl, useClass: CustomMatPaginatorIntl },
CookieService,
OverlayModule,
DeviceDetectorUtils,
MatDatepickerModule,
MatNativeDateModule,
{ provide: LOCALE_ID, useValue: 'fr-FR' },
{ provide: DateAdapter, useClass: MomentDateAdapter },
{ provide: MAT_DATE_FORMATS, useValue: CUSTOM_DATE_FORMAT },
{ provide: MAT_DATE_LOCALE, useValue: 'fr-FR' }
],
bootstrap: [AppComponent]
})
export class AppModule {
}
app-routing-module.ts
All the application is on a single route (/home
) which dynamically update components.
const routes: Routes = [
{ path: 'home', component: HomeComponent, loadChildren: () => {
return import('./features/feature1/feature1.module').then(m => m.Feature1Module),
import('./features/feature2/feature2.module').then(m => m.Feature2Module),
//...
import('./navigation/navigation.module').then(m => m.NavigationModule),
import('./features/home/home.module').then(m => m.HomeModule);
}},
{ path: 'password-creation/:m/:p', component: PasswordCreationComponent , loadChildren: () => {
return import('./features/user-management/user-management.module').then(m => m.UserManagementModule);
}},
{ path: '**', redirectTo: 'home', pathMatch: 'prefix' }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
home.module.ts
@NgModule({
declarations: [HomeComponent],
imports: [
CommonModule, SharedModule, HomeRoutingModule
]
})
export class HomeModule { }
home-routing-module.ts
const routes: Routes = [
{
path: '',
component: HomeComponent
}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class HomeRoutingModule {}
shared.module.ts
const PIPES = [
// All my shared pipes
];
const COMPONENTS = [
// All my shared components
];
const DIRECTIVES = [
// All my shared directives
];
const MODULES = [
CommonModule,
PortalModule,
ReactiveFormsModule,
FormsModule,
LayoutModule,
RouterModule,
// All my shared modules
];
const PROVIDERS = [
// All my shared providers
];
@NgModule({
declarations: [...PIPES, ...COMPONENTS, ...DIRECTIVES],
imports: [...MODULES],
exports: [...PIPES, ...COMPONENTS, ...DIRECTIVES, ...MODULES],
providers: [...PROVIDERS]
})
export class SharedModule { }
Every feature module imports the sharedModule
.
I'm guessing this is due to lazy loading but the CommonModule
and BrowserModule
are imported in the AppModule
so they should not be lazy loaded, right?
ng version
Angular CLI: 9.1.15
Node: 14.16.1
OS: win32 x64
Angular: 9.1.13
... animations, common, compiler, compiler-cli, core, forms
... language-service, localize, platform-browser
... platform-browser-dynamic, router
Ivy Workspace: Yes
Package Version
------------------------------------------------------------
@angular-devkit/architect 0.901.15
@angular-devkit/build-angular 0.901.15
@angular-devkit/build-optimizer 0.901.15
@angular-devkit/build-webpack 0.901.15
@angular-devkit/core 9.1.15
@angular-devkit/schematics 9.1.15
@angular/cdk 9.2.4
@angular/cli 9.1.15
@angular/material 9.2.4
@angular/material-moment-adapter 11.2.13
@ngtools/webpack 9.1.15
@schematics/angular 9.1.15
@schematics/update 0.901.15
rxjs 6.5.5
typescript 3.8.3
webpack 4.42.0
Thanks for any insights.