I try to find a good way to structure my Angular 2 app. The Angular 2 styleguide recommends the creation of a core module. If I understand right the goal of the core module is to collect single-use classes and components and keep the root module slim. There is also written that I should import all modules required by the assets into the core module.
I am a little bit confused when it comes to third party libraries that need to be included with the Method forRoot() (Like NgBootstrap or angular2-notifications). Normally the forRoot() Method should only be called in the root module. Should I include such modules inside the root Module or in the core Module?
In the following example I need to do some configuration for angular2-notifications. To keep my root module slim I imported SimpleNotifications inside the core module.
- Is this the correct way? To make the app work I still needed to import SimpleNotificationsModule.forRoot() in the App Module.
- Why do I need to import SimpleNotificationsModule again in the Core Module. Shouldn't it be provided by the app module. I thought that thanks to forRoot() they core module uses the same imported modules as ther app module?
- Should I import SimpleNotifications inside core module, if yes? Should I really call the forRoot() Method there?
App Module
...
import {SimpleNotificationsModule} from 'angular2-notifications';
@NgModule({
declarations: [...],
imports: [
BrowserModule,
CoreModule,
NgbModule.forRoot(),
SimpleNotificationsModule.forRoot(),
],
bootstrap: [AppComponent]
})
export class AppModule {}
App Component
...
<notifications></notifications>
Core Module
import {SimpleNotificationsModule} from 'angular2-notifications';
import {NotificationsComponent} from
'./notifications/notifications.component';
@NgModule({
imports: [
CommonModule,
FormsModule,
HttpModule,
RouterModule,
SimpleNotificationsModule
],
declarations: [...],
exports: [NotificationsComponent]
})
export class CoreModule {
constructor(@Optional() @SkipSelf() parentModule: CoreModule) {
throwIfAlreadyLoaded(parentModule, 'core module');
}
}
NotificationsComponent
import {Component, ViewEncapsulation} from '@angular/core';
@Component({
selector: 'notifications',
template: `<simple-notifications [options]="notificationOptions">
</simple-notifications>`,
styleUrls: ['./notifications.component.css'],
encapsulation: ViewEncapsulation.None
})
export class NotificationsComponent {
public notificationOptions = {
position: ['top', 'right'],
timeOut: 5000,
maxStack: 5,
lastOnBottom: true
};
}