fix(router): throw an error when encounter undefined route (#12389)
This commit is contained in:
parent
3052fb234f
commit
77dc1ab675
|
@ -350,10 +350,26 @@ export interface Route {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function validateConfig(config: Routes): void {
|
export function validateConfig(config: Routes): void {
|
||||||
config.forEach(validateNode);
|
// forEach doesn't iterate undefined values
|
||||||
|
for (let i = 0; i < config.length; i++) {
|
||||||
|
validateNode(config[i]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function validateNode(route: Route): void {
|
function validateNode(route: Route): void {
|
||||||
|
if (!route) {
|
||||||
|
throw new Error(`
|
||||||
|
Invalid route configuration: Encountered undefined route.
|
||||||
|
The reason might be an extra comma.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
const routes: Routes = [
|
||||||
|
{ path: '', redirectTo: '/dashboard', pathMatch: 'full' },
|
||||||
|
{ path: 'dashboard', component: DashboardComponent },, << two commas
|
||||||
|
{ path: 'detail/:id', component: HeroDetailComponent }
|
||||||
|
];
|
||||||
|
`);
|
||||||
|
}
|
||||||
if (Array.isArray(route)) {
|
if (Array.isArray(route)) {
|
||||||
throw new Error(`Invalid route configuration: Array cannot be specified`);
|
throw new Error(`Invalid route configuration: Array cannot be specified`);
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,6 @@
|
||||||
|
|
||||||
import {APP_BASE_HREF, HashLocationStrategy, Location, LocationStrategy, PathLocationStrategy, PlatformLocation} from '@angular/common';
|
import {APP_BASE_HREF, HashLocationStrategy, Location, LocationStrategy, PathLocationStrategy, PlatformLocation} from '@angular/common';
|
||||||
import {ANALYZE_FOR_ENTRY_COMPONENTS, APP_BOOTSTRAP_LISTENER, ApplicationRef, Compiler, Inject, Injector, ModuleWithProviders, NgModule, NgModuleFactoryLoader, OpaqueToken, Optional, Provider, SkipSelf, SystemJsNgModuleLoader} from '@angular/core';
|
import {ANALYZE_FOR_ENTRY_COMPONENTS, APP_BOOTSTRAP_LISTENER, ApplicationRef, Compiler, Inject, Injector, ModuleWithProviders, NgModule, NgModuleFactoryLoader, OpaqueToken, Optional, Provider, SkipSelf, SystemJsNgModuleLoader} from '@angular/core';
|
||||||
|
|
||||||
import {Route, Routes} from './config';
|
import {Route, Routes} from './config';
|
||||||
import {RouterLink, RouterLinkWithHref} from './directives/router_link';
|
import {RouterLink, RouterLinkWithHref} from './directives/router_link';
|
||||||
import {RouterLinkActive} from './directives/router_link_active';
|
import {RouterLinkActive} from './directives/router_link_active';
|
||||||
|
|
|
@ -15,6 +15,12 @@ describe('config', () => {
|
||||||
validateConfig([{path: 'a', redirectTo: 'b'}, {path: 'b', component: ComponentA}]);
|
validateConfig([{path: 'a', redirectTo: 'b'}, {path: 'b', component: ComponentA}]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should throw for undefined route', () => {
|
||||||
|
expect(() => {
|
||||||
|
validateConfig([{path: 'a', component: ComponentA}, , {path: 'b', component: ComponentB}]);
|
||||||
|
}).toThrowError();
|
||||||
|
});
|
||||||
|
|
||||||
it('should throw when Array is passed', () => {
|
it('should throw when Array is passed', () => {
|
||||||
expect(() => {
|
expect(() => {
|
||||||
validateConfig([
|
validateConfig([
|
||||||
|
|
Loading…
Reference in New Issue