Skip to content

Commit 6af8667

Browse files
committed
feat(module): add for root module guard implementation
1 parent bdb2449 commit 6af8667

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

projects/elements/src/lib/lazy-elements/lazy-elements.module.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import {
44
Inject,
55
ModuleWithProviders,
66
ANALYZE_FOR_ENTRY_COMPONENTS,
7-
Type
7+
Type,
8+
SkipSelf
89
} from '@angular/core';
910
import { CommonModule } from '@angular/common';
1011

@@ -16,9 +17,19 @@ import {
1617
} from './lazy-elements-loader.service';
1718
import {
1819
LAZY_ELEMENT_ROOT_OPTIONS,
19-
LAZY_ELEMENT_CONFIGS
20+
LAZY_ELEMENT_CONFIGS,
21+
LAZY_ELEMENT_ROOT_GUARD
2022
} from './lazy-elements.tokens';
2123

24+
export function createLazyElementRootGuard(options: LazyElementModuleOptions) {
25+
if (options) {
26+
throw new TypeError(
27+
`LazyElementsModule.forRoot() called twice. Feature modules should use ThemeModule.forFeature() instead.`
28+
);
29+
}
30+
return 'guarded';
31+
}
32+
2233
@NgModule({
2334
declarations: [LazyElementDirective, LazyElementDynamicDirective],
2435
imports: [CommonModule],
@@ -44,6 +55,11 @@ export class LazyElementsModule {
4455
provide: ANALYZE_FOR_ENTRY_COMPONENTS,
4556
useValue: options,
4657
multi: true
58+
},
59+
{
60+
provide: LAZY_ELEMENT_ROOT_GUARD,
61+
useFactory: createLazyElementRootGuard,
62+
deps: [[LAZY_ELEMENT_CONFIGS, new Optional(), new SkipSelf()]]
4763
}
4864
]
4965
};
@@ -72,7 +88,10 @@ export class LazyElementsModule {
7288
lazyElementsLoaderService: LazyElementsLoaderService,
7389
@Optional()
7490
@Inject(LAZY_ELEMENT_CONFIGS)
75-
elementConfigsMultiProvider: ElementConfig[][]
91+
elementConfigsMultiProvider: ElementConfig[][],
92+
@Optional()
93+
@Inject(LAZY_ELEMENT_ROOT_GUARD)
94+
guard: any
7695
) {
7796
if (elementConfigsMultiProvider && elementConfigsMultiProvider.length) {
7897
const lastAddedConfigs =

0 commit comments

Comments
 (0)