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 {Observable} from 'rxjs/Observable';
import {PRIMARY_OUTLET} from './shared';
/**
* @whatItDoes Represents router configuration.
@ -320,6 +320,10 @@ function validateNode(route: Route): void {
if (Array.isArray(route)) {
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) {
throw new Error(
`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 {PRIMARY_OUTLET} from '../shared';
/**
* @whatItDoes Acts as a placeholder that Angular dynamically fills based on the current router
* state.
@ -79,6 +77,10 @@ export class RouterOutlet implements OnDestroy {
activatedRoute: ActivatedRoute, loadedResolver: ComponentFactoryResolver,
loadedInjector: Injector, providers: ResolvedReflectiveProvider[],
outletMap: RouterOutletMap): void {
if (this.isActivated) {
throw new Error('Cannot activate an already activated outlet');
}
this.outletMap = outletMap;
this._activatedRoute = activatedRoute;

View File

@ -7,6 +7,7 @@
*/
import {validateConfig} from '../src/config';
import {PRIMARY_OUTLET} from '../src/shared';
describe('config', () => {
describe('validateConfig', () => {
@ -80,6 +81,16 @@ describe('config', () => {
.toThrowError(
/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();
});
});
});