Skip to content

Commit 7d4ac6d

Browse files
fix(loadNgModule): Better error message when lazy loaded NgModule contains no state defs
1 parent dca0f61 commit 7d4ac6d

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

src/ng2/lazyLoadNgModule.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,25 +105,39 @@ export function applyNgModule(transition: Transition, ng2Module: NgModuleRef<any
105105
let originalName = transition.to().name;
106106
let originalState = uiRouter.stateRegistry.get(originalName);
107107

108-
let rootModules: RootModule[] = injector.get(UIROUTER_ROOT_MODULE);
109-
let parentRootModules: RootModule[] = parentInjector.get(UIROUTER_ROOT_MODULE);
110-
let newRootModules = rootModules.filter(module => parentRootModules.indexOf(module) === -1);
108+
let newRootModules: RootModule[] = multiProviderParentChildDelta(parentInjector, injector, UIROUTER_ROOT_MODULE);
111109

112110
if (newRootModules.length) {
113-
console.log(rootModules);
111+
console.log(newRootModules);
114112
throw new Error('Lazy loaded modules should not contain a UIRouterModule.forRoot() module');
115113
}
116114

117-
let modules: StatesModule[] = injector.get(UIROUTER_MODULE_TOKEN);
118-
modules.forEach(module => applyModuleConfig(uiRouter, injector, module));
115+
let newModules: RootModule[] = multiProviderParentChildDelta(parentInjector, injector, UIROUTER_MODULE_TOKEN);
116+
newModules.forEach(module => applyModuleConfig(uiRouter, injector, module));
119117

120118
let replacementState = uiRouter.stateRegistry.get(originalName);
121119
if (replacementState === originalState) {
122-
throw new Error(`The module that was lazy loaded by activating ${originalName} should also have a ui-router state named '${originalName}'`);
120+
throw new Error(`The Future State named '${originalName}' lazy loaded an NgModule. That NgModule should also have a UIRouterModule.forChild() state named '${originalName}' to replace the Future State, but it did not.`);
123121
}
124122

125123
// Supply the newly loaded states with the Injector from the lazy loaded NgModule
126124
replacementState.$$state().resolvables.push(Resolvable.fromData(NATIVE_INJECTOR_TOKEN, injector));
127125

128126
return {};
129127
}
128+
129+
/**
130+
* Returns the new dependency injection values from the Child Injector
131+
*
132+
* When a DI token is defined as multi: true, the child injector
133+
* can add new values for the token.
134+
*
135+
* This function returns the values added by the child injector, and excludes all values from the parent injector.
136+
*
137+
* @internalapi
138+
*/
139+
export function multiProviderParentChildDelta(parent: Injector, child: Injector, token: any) {
140+
let childVals: RootModule[] = child.get(token);
141+
let parentVals: RootModule[] = parent.get(token);
142+
return childVals.filter(val => parentVals.indexOf(val) === -1);
143+
}

0 commit comments

Comments
 (0)