From 313ca4def29bdc178c0ef03a26456df349862a56 Mon Sep 17 00:00:00 2001 From: IT-Ideas Date: Fri, 3 Feb 2017 14:44:55 +0100 Subject: [PATCH 1/2] Usage in multiple shared modules Update in order to use the same instance of the notifications service in different shared modules. forRoot() is called in the main app module only. --- src/simple-notifications.module.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/simple-notifications.module.ts b/src/simple-notifications.module.ts index d3f58f2..0a66225 100644 --- a/src/simple-notifications.module.ts +++ b/src/simple-notifications.module.ts @@ -8,8 +8,16 @@ import {MaxPipe} from './max.pipe'; @NgModule({ imports: [CommonModule], declarations: [SimpleNotificationsComponent, NotificationComponent, MaxPipe], - providers: [NotificationsService], + providers: [], exports: [SimpleNotificationsComponent] }) export class SimpleNotificationsModule { + public static forRoot(): ModuleWithProviders { + return { + ngModule: SimpleNotificationsModule, + providers: [ + NotificationsService + ] + }; + } } From c8f2e8bf7712c722cd4627d905bdc23dd52af45e Mon Sep 17 00:00:00 2001 From: IT-Ideas Date: Fri, 3 Feb 2017 14:50:01 +0100 Subject: [PATCH 2/2] Updated import accordingly --- src/simple-notifications.module.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/simple-notifications.module.ts b/src/simple-notifications.module.ts index 0a66225..b881870 100644 --- a/src/simple-notifications.module.ts +++ b/src/simple-notifications.module.ts @@ -1,4 +1,4 @@ -import {NgModule} from '@angular/core'; +import {NgModule, ModuleWithProviders} from '@angular/core'; import {CommonModule} from '@angular/common'; import {NotificationsService} from './notifications.service'; import {SimpleNotificationsComponent} from './simple-notifications.component';