fix(router): throw an error when encounter undefined route (#12389)

This commit is contained in:
Dzmitry Shylovich 2016-11-11 01:55:10 +03:00 committed by Victor Berchet
parent 3052fb234f
commit 77dc1ab675
3 changed files with 23 additions and 2 deletions

View File

@ -350,10 +350,26 @@ export interface Route {
}
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 {
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)) {
throw new Error(`Invalid route configuration: Array cannot be specified`);
}

View File

@ -8,7 +8,6 @@
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 {Route, Routes} from './config';
import {RouterLink, RouterLinkWithHref} from './directives/router_link';
import {RouterLinkActive} from './directives/router_link_active';

View File

@ -15,6 +15,12 @@ describe('config', () => {
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', () => {
expect(() => {
validateConfig([