Skip to content

Commit a501f40

Browse files
committed
fix(aot): Fixed error occuring when using NotifierModule.forRoot with
AoT - Extracted NotifierConfig provider into a factory - Added a new DI Token for custom options Closes #5
1 parent 53091cf commit a501f40

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

src/notifier.module.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { CommonModule } from '@angular/common';
2-
import { ModuleWithProviders, NgModule } from '@angular/core';
2+
import { ModuleWithProviders, NgModule, OpaqueToken } from '@angular/core';
33

44
import { NotifierContainerComponent } from './components/notifier-container.component';
55
import { NotifierNotificationComponent } from './components/notifier-notification.component';
@@ -8,6 +8,26 @@ import { NotifierAnimationService } from './services/notifier-animation.service'
88
import { NotifierQueueService } from './services/notifier-queue.service';
99
import { NotifierService } from './services/notifier.service';
1010

11+
// tslint:disable variable-name
12+
/**
13+
* Dependency Injection token for custom notifier options
14+
*/
15+
export const CustomNotifierOptions: OpaqueToken = new OpaqueToken( 'NotifierOptions' );
16+
// tslint:enable variable-name
17+
18+
/**
19+
* Factory for creating a notifier configuration object
20+
*
21+
* Sidenote:
22+
* This functionality had to be extracted from the NotifierModule.forRoot function, described below. Otherwhise, the Angular AoT compiler
23+
* would throw errors. For further details, also see:
24+
* - Angular issue #11262 <https://github.com/angular/angular/issues/11262>
25+
* - Angular issue #10789 <https://github.com/angular/angular/issues/10789>
26+
*/
27+
export function notifierConfigFactory( options: NotifierOptions ): NotifierConfig {
28+
return new NotifierConfig( options );
29+
}
30+
1131
/**
1232
* Notifier module
1333
*/
@@ -38,8 +58,15 @@ export class NotifierModule {
3858
ngModule: NotifierModule,
3959
providers: [
4060
{
61+
provide: CustomNotifierOptions,
62+
useValue: options
63+
},
64+
{
65+
deps: [
66+
CustomNotifierOptions
67+
],
4168
provide: NotifierConfig,
42-
useValue: new NotifierConfig( options )
69+
useFactory: notifierConfigFactory
4370
}
4471
]
4572
};

0 commit comments

Comments
 (0)