fix(router): top-levels do not work in ngIf

This commit is contained in:
vsavkin 2016-06-26 11:52:32 -07:00
parent 855f3afb28
commit d20488752b
5 changed files with 44 additions and 11 deletions

View File

@ -28,7 +28,7 @@ import {UrlChangeListener} from './platform_location';
* @stable * @stable
*/ */
export abstract class LocationStrategy { export abstract class LocationStrategy {
abstract path(includeHash: boolean): string; abstract path(includeHash?: boolean): string;
abstract prepareExternalUrl(internal: string): string; abstract prepareExternalUrl(internal: string): string;
abstract pushState(state: any, title: string, url: string, queryParams: string): void; abstract pushState(state: any, title: string, url: string, queryParams: string): void;
abstract replaceState(state: any, title: string, url: string, queryParams: string): void; abstract replaceState(state: any, title: string, url: string, queryParams: string): void;

View File

@ -276,7 +276,7 @@ export class Router {
return new Promise((resolvePromise, rejectPromise) => { return new Promise((resolvePromise, rejectPromise) => {
let updatedUrl: UrlTree; let updatedUrl: UrlTree;
let state: RouterState; let state: RouterState;
let navigationIsSuccessful; let navigationIsSuccessful: boolean;
applyRedirects(url, this.config) applyRedirects(url, this.config)
.mergeMap(u => { .mergeMap(u => {
updatedUrl = u; updatedUrl = u;
@ -494,6 +494,7 @@ class ActivateRoutes {
const currRoot = this.currState ? this.currState._root : null; const currRoot = this.currState ? this.currState._root : null;
pushQueryParamsAndFragment(this.futureState); pushQueryParamsAndFragment(this.futureState);
advanceActivatedRoute(this.futureState.root);
this.activateChildRoutes(futureRoot, currRoot, parentOutletMap); this.activateChildRoutes(futureRoot, currRoot, parentOutletMap);
} }

View File

@ -518,10 +518,28 @@ describe('Integration', () => {
it('should support top-level link', it('should support top-level link',
fakeAsync( fakeAsync(
inject([Router, TestComponentBuilder], (router: Router, tcb: TestComponentBuilder) => { inject([Router, TestComponentBuilder], (router: Router, tcb: TestComponentBuilder) => {
let fixture = tcb.createFakeAsync(AbsoluteLinkCmp); const fixture = tcb.createFakeAsync(RelativeLinkInIfCmp);
advance(fixture); advance(fixture);
expect(fixture.debugElement.nativeElement).toHaveText('link'); router.resetConfig(
[{path: 'simple', component: SimpleCmp}, {path: '', component: BlankCmp}]);
router.navigateByUrl('/');
advance(fixture);
expect(fixture.debugElement.nativeElement).toHaveText(' ');
const cmp = fixture.debugElement.componentInstance;
cmp.show = true;
advance(fixture);
expect(fixture.debugElement.nativeElement).toHaveText('link ');
const native = fixture.debugElement.nativeElement.querySelector('a');
expect(native.getAttribute('href')).toEqual('/simple');
native.click();
advance(fixture);
expect(fixture.debugElement.nativeElement).toHaveText('link simple');
}))); })));
it('should support query params and fragments', it('should support query params and fragments',
@ -731,13 +749,13 @@ describe('Integration', () => {
advance(fixture); advance(fixture);
expect(location.path()).toEqual('/team/22'); expect(location.path()).toEqual('/team/22');
let successStatus; let successStatus: boolean;
router.navigateByUrl('/team/33').then(res => successStatus = res); router.navigateByUrl('/team/33').then(res => successStatus = res);
advance(fixture); advance(fixture);
expect(location.path()).toEqual('/team/33'); expect(location.path()).toEqual('/team/33');
expect(successStatus).toEqual(true); expect(successStatus).toEqual(true);
let canceledStatus; let canceledStatus: boolean;
router.navigateByUrl('/team/44').then(res => canceledStatus = res); router.navigateByUrl('/team/44').then(res => canceledStatus = res);
advance(fixture); advance(fixture);
expect(location.path()).toEqual('/team/33'); expect(location.path()).toEqual('/team/33');
@ -1017,6 +1035,16 @@ class DummyLinkWithParentCmp {
class RelativeLinkCmp { class RelativeLinkCmp {
} }
@Component({
selector: 'link-cmp',
template:
`<div *ngIf="show"><a [routerLink]="['./simple']">link</a></div> <router-outlet></router-outlet>`,
directives: ROUTER_DIRECTIVES
})
class RelativeLinkInIfCmp {
show: boolean = false;
}
@Component({ @Component({
selector: 'link-cmp', selector: 'link-cmp',
template: `<a [routerLink]="['../simple']" [queryParams]="{q: '1'}" fragment="f">link</a>`, template: `<a [routerLink]="['../simple']" [queryParams]="{q: '1'}" fragment="f">link</a>`,
@ -1040,7 +1068,9 @@ class CollectParamsCmp {
a.url.forEach(u => this.urls.push(u)); a.url.forEach(u => this.urls.push(u));
} }
recordedUrls(): string[] { return this.urls.map(a => a.map(p => p.path).join('/')); } recordedUrls(): string[] {
return this.urls.map((a: any) => a.map((p: any) => p.path).join('/'));
}
} }
@Component({selector: 'blank-cmp', template: ``, directives: ROUTER_DIRECTIVES}) @Component({selector: 'blank-cmp', template: ``, directives: ROUTER_DIRECTIVES})

View File

@ -8,6 +8,8 @@
"module": "commonjs", "module": "commonjs",
"moduleResolution": "node", "moduleResolution": "node",
"noEmitOnError": false, "noEmitOnError": false,
"noImplicitAny": true,
"noImplicitReturns": true,
"outDir": "../../../dist/all/@angular/router/", "outDir": "../../../dist/all/@angular/router/",
"paths": { "paths": {
"@angular/*": ["../../../dist/all/@angular/*"] "@angular/*": ["../../../dist/all/@angular/*"]

View File

@ -197,7 +197,7 @@ export declare class HashLocationStrategy extends LocationStrategy {
forward(): void; forward(): void;
getBaseHref(): string; getBaseHref(): string;
onPopState(fn: UrlChangeListener): void; onPopState(fn: UrlChangeListener): void;
path(): string; path(includeHash?: boolean): string;
prepareExternalUrl(internal: string): string; prepareExternalUrl(internal: string): string;
pushState(state: any, title: string, path: string, queryParams: string): void; pushState(state: any, title: string, path: string, queryParams: string): void;
replaceState(state: any, title: string, path: string, queryParams: string): void; replaceState(state: any, title: string, path: string, queryParams: string): void;
@ -231,7 +231,7 @@ export declare class Location {
go(path: string, query?: string): void; go(path: string, query?: string): void;
isCurrentPathEqualTo(path: string, query?: string): boolean; isCurrentPathEqualTo(path: string, query?: string): boolean;
normalize(url: string): string; normalize(url: string): string;
path(): string; path(includeHash?: boolean): string;
prepareExternalUrl(url: string): string; prepareExternalUrl(url: string): string;
replaceState(path: string, query?: string): void; replaceState(path: string, query?: string): void;
subscribe(onNext: (value: any) => void, onThrow?: (exception: any) => void, onReturn?: () => void): Object; subscribe(onNext: (value: any) => void, onThrow?: (exception: any) => void, onReturn?: () => void): Object;
@ -246,7 +246,7 @@ export declare abstract class LocationStrategy {
abstract forward(): void; abstract forward(): void;
abstract getBaseHref(): string; abstract getBaseHref(): string;
abstract onPopState(fn: UrlChangeListener): void; abstract onPopState(fn: UrlChangeListener): void;
abstract path(): string; abstract path(includeHash?: boolean): string;
abstract prepareExternalUrl(internal: string): string; abstract prepareExternalUrl(internal: string): string;
abstract pushState(state: any, title: string, url: string, queryParams: string): void; abstract pushState(state: any, title: string, url: string, queryParams: string): void;
abstract replaceState(state: any, title: string, url: string, queryParams: string): void; abstract replaceState(state: any, title: string, url: string, queryParams: string): void;
@ -495,7 +495,7 @@ export declare class PathLocationStrategy extends LocationStrategy {
forward(): void; forward(): void;
getBaseHref(): string; getBaseHref(): string;
onPopState(fn: UrlChangeListener): void; onPopState(fn: UrlChangeListener): void;
path(): string; path(includeHash?: boolean): string;
prepareExternalUrl(internal: string): string; prepareExternalUrl(internal: string): string;
pushState(state: any, title: string, url: string, queryParams: string): void; pushState(state: any, title: string, url: string, queryParams: string): void;
replaceState(state: any, title: string, url: string, queryParams: string): void; replaceState(state: any, title: string, url: string, queryParams: string): void;