From 06e4ca4bb3bcee85dfad4fe5ef0551a6882c04ec Mon Sep 17 00:00:00 2001 From: vsavkin Date: Fri, 22 Jul 2016 17:14:04 -0700 Subject: [PATCH] fix(router): advance query params and fragment after advanced routes --- modules/@angular/router/src/router.ts | 2 +- modules/@angular/router/test/router.spec.ts | 41 +++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/modules/@angular/router/src/router.ts b/modules/@angular/router/src/router.ts index b433b87929..fb82c32511 100644 --- a/modules/@angular/router/src/router.ts +++ b/modules/@angular/router/src/router.ts @@ -644,9 +644,9 @@ class ActivateRoutes { activate(parentOutletMap: RouterOutletMap): void { const futureRoot = this.futureState._root; const currRoot = this.currState ? this.currState._root : null; - pushQueryParamsAndFragment(this.futureState); advanceActivatedRoute(this.futureState.root); this.activateChildRoutes(futureRoot, currRoot, parentOutletMap); + pushQueryParamsAndFragment(this.futureState); } private activateChildRoutes( diff --git a/modules/@angular/router/test/router.spec.ts b/modules/@angular/router/test/router.spec.ts index 948b65cb1b..591540fa61 100644 --- a/modules/@angular/router/test/router.spec.ts +++ b/modules/@angular/router/test/router.spec.ts @@ -251,6 +251,47 @@ describe('Integration', () => { expect(fixture.debugElement.nativeElement).toHaveText('query: 2 fragment: fragment2'); }))); + it('should not push query params into components that will be deactivated', + fakeAsync( + inject([Router, TestComponentBuilder], (router: Router, tcb: TestComponentBuilder) => { + @Component({template: ''}) + class ComponentRecordingQueryParams { + recordedQueryParams: any[] = []; + subscription: any; + constructor(r: Router) { + this.subscription = + r.routerState.queryParams.subscribe(r => this.recordedQueryParams.push(r)); + } + + ngOnDestroy() { this.subscription.unsubscribe(); } + } + + @Component({ + template: '', + precompile: [SimpleCmp, ComponentRecordingQueryParams] + }) + class RootCmp { + } + + router.resetConfig([ + {path: '', component: ComponentRecordingQueryParams}, + {path: 'simple', component: SimpleCmp} + ]); + + const fixture = createRoot(tcb, router, RootCmp); + router.navigateByUrl('/?a=v1'); + advance(fixture); + + const c = fixture.debugElement.children[1].componentInstance; + + expect(c.recordedQueryParams).toEqual([{}, {a: 'v1'}]); + + router.navigateByUrl('/simple?a=v2'); + advance(fixture); + + expect(c.recordedQueryParams).toEqual([{}, {a: 'v1'}]); + }))); + it('should push params only when they change', fakeAsync( inject([Router, TestComponentBuilder], (router: Router, tcb: TestComponentBuilder) => {