fix(router): disallow component routes with named outlets

Closes #11208, #11082
This commit is contained in:
vsavkin 2016-10-20 15:00:15 -07:00
parent fc60fa790c
commit 8f2fa0f766
3 changed files with 20 additions and 3 deletions

View File

@ -8,7 +8,7 @@
import {Type} from '@angular/core'; import {Type} from '@angular/core';
import {Observable} from 'rxjs/Observable'; import {Observable} from 'rxjs/Observable';
import {PRIMARY_OUTLET} from './shared';
/** /**
* @whatItDoes Represents router configuration. * @whatItDoes Represents router configuration.
@ -320,6 +320,10 @@ function validateNode(route: Route): void {
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`);
} }
if (route.component === undefined && (route.outlet && route.outlet !== PRIMARY_OUTLET)) {
throw new Error(
`Invalid route configuration of route '${route.path}': a componentless route cannot have a named outlet set`);
}
if (!!route.redirectTo && !!route.children) { if (!!route.redirectTo && !!route.children) {
throw new Error( throw new Error(
`Invalid configuration of route '${route.path}': redirectTo and children cannot be used together`); `Invalid configuration of route '${route.path}': redirectTo and children cannot be used together`);

View File

@ -12,8 +12,6 @@ import {RouterOutletMap} from '../router_outlet_map';
import {ActivatedRoute} from '../router_state'; import {ActivatedRoute} from '../router_state';
import {PRIMARY_OUTLET} from '../shared'; import {PRIMARY_OUTLET} from '../shared';
/** /**
* @whatItDoes Acts as a placeholder that Angular dynamically fills based on the current router * @whatItDoes Acts as a placeholder that Angular dynamically fills based on the current router
* state. * state.
@ -79,6 +77,10 @@ export class RouterOutlet implements OnDestroy {
activatedRoute: ActivatedRoute, loadedResolver: ComponentFactoryResolver, activatedRoute: ActivatedRoute, loadedResolver: ComponentFactoryResolver,
loadedInjector: Injector, providers: ResolvedReflectiveProvider[], loadedInjector: Injector, providers: ResolvedReflectiveProvider[],
outletMap: RouterOutletMap): void { outletMap: RouterOutletMap): void {
if (this.isActivated) {
throw new Error('Cannot activate an already activated outlet');
}
this.outletMap = outletMap; this.outletMap = outletMap;
this._activatedRoute = activatedRoute; this._activatedRoute = activatedRoute;

View File

@ -7,6 +7,7 @@
*/ */
import {validateConfig} from '../src/config'; import {validateConfig} from '../src/config';
import {PRIMARY_OUTLET} from '../src/shared';
describe('config', () => { describe('config', () => {
describe('validateConfig', () => { describe('validateConfig', () => {
@ -80,6 +81,16 @@ describe('config', () => {
.toThrowError( .toThrowError(
/Invalid configuration of route 'a': pathMatch can only be set to 'prefix' or 'full'/); /Invalid configuration of route 'a': pathMatch can only be set to 'prefix' or 'full'/);
}); });
it('should throw when pathPatch is invalid', () => {
expect(() => { validateConfig([{path: 'a', outlet: 'aux', children: []}]); })
.toThrowError(
/Invalid route configuration of route 'a': a componentless route cannot have a named outlet set/);
expect(() => validateConfig([{path: 'a', outlet: '', children: []}])).not.toThrow();
expect(() => validateConfig([{path: 'a', outlet: PRIMARY_OUTLET, children: []}]))
.not.toThrow();
});
}); });
}); });