I have following example application layout:
app.module
|
|-- dashboard.module (lazy loaded)
|-- dashboard-routing.module (imported by dashboard.module)
|-- dashboard.service (provided by dashboard.module)
|-- dashboard.component (depends on dashboard.service)
|
|-- notifications.module (lazy loaded)
|-- notifications-routing.module (imported by notifications.module)
|-- notifications.service (provided by notifications.module)
|-- notifications.component (depends on notifications.service)
|
|-- my-notifications-dashboard-widget.component <- this should be added
|
|-- ...
Every module (dashboard, notifications, ...) is getting lazy loaded.
Now I'd like to add some MyNotificationsDashboardWidgetComponent, which depends on the notifications.service. This widget then should get injected within the dashboard.service.
This works well, as long as the provided WidgetComponent is inside the dashboard.module. But if I put the component within the lazy loaded notifications.module, it of course doesn't work.
Now I'd like to know, how to solve this in a good, module oriented way.
My requirements:
There should only be one
notifications.service, which should be used bynotifications.componentandmy-notifications-dashboard-widget.component.The big modules (
dashboard,notifications) should be lazy loaded.The
dashboard.moduleshouldn't know anything about thenotifications.moduleand / or themy-notifications-dashboard-widget.component.
Ideas:
I guess, that I need some eager loaded module, which contains my-notifications-dashboard-widget.component. So that this component and its provider is known before the dashboard.module is getting loaded.
But then I'd have to put the notifications.service in this module, too, I guess?!
Some code snippets of the current (non-working) approach:
dashboard.service:
@Injectable()
export class DashboardService {
constructor(@Inject(DASHBOARD_WIDGET) widgets) {
// inject all provided widgets
widgets.forEach(w => console.log(w.type));
}
}
notifications.module:
@NgModule({
imports: [ NotificationsRoutingModule ],
declarations: [ NotificationsComponent ],
providers: [
NotificationsService,
// this is currently not working, because of lazy loading
{
provide: DASHBOARD_WIDGET,
useValue: {
type: 'my-notifications'
component: MyNotificationsDashboardWidgetComponent
},
multi: true
}
],
entryComponents: [ MyNotificationsDashboardWidgetComponent ]
})
export class NotificationsModule { }
notifications.component:
@Component({ ... })
export class NotificationsComponent {
constructor(service: NotificationsService) { ... }
}
my-notifications-dashboard-widget.component:
@Component({ ... })
export class MyNotificationsDashboardWidgetComponent {
constructor(service: NotificationsService) { ... }
}