fix(angular): preserve queryParams and fragment when going back (#18298)

fixes #16744
This commit is contained in:
Liam DeBeasi
2019-05-20 11:56:02 -04:00
committed by GitHub
parent da38647478
commit bdd5109dbe
4 changed files with 27 additions and 3 deletions

View File

@ -141,6 +141,20 @@ export class IonRouterOutlet implements OnDestroy, OnInit {
if (this.activated) {
if (this.activatedView) {
this.activatedView.savedData = new Map(this.getContext()!.children['contexts']);
/**
* Ensure we are saving the NavigationExtras
* data otherwise it will be lost
*/
this.activatedView.savedExtras = {};
const context = this.getContext()!;
if (context.route) {
const contextSnapshot = context.route.snapshot;
this.activatedView.savedExtras.queryParams = contextSnapshot.queryParams;
this.activatedView.savedExtras.fragment = contextSnapshot.fragment;
}
}
const c = this.component;
this.activatedView = null;

View File

@ -132,7 +132,7 @@ export class StackController {
}
}
return this.navCtrl.navigateBack(url).then(() => true);
return this.navCtrl.navigateBack(url, view.savedExtras).then(() => true);
});
}

View File

@ -1,5 +1,5 @@
import { ComponentRef } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { ActivatedRoute, NavigationExtras, Router } from '@angular/router';
import { NavDirection, RouterDirection } from '@ionic/core';
export function insertView(views: RouteView[], view: RouteView, direction: RouterDirection) {
@ -94,5 +94,6 @@ export interface RouteView {
element: HTMLElement;
ref: ComponentRef<any>;
savedData?: any;
savedExtras?: NavigationExtras;
unlistenEvents: () => void;
}

View File

@ -184,7 +184,16 @@ export class NavController {
if (Array.isArray(url)) {
return this.router!.navigate(url, options);
} else {
return this.router!.navigateByUrl(url, options);
/**
* navigateByUrl ignores any properties that
* would change the url, so things like queryParams
* would be ignored unless we create a url tree
* More Info: https://github.com/angular/angular/issues/18798
*/
return this.router!.navigateByUrl(
this.router!.createUrlTree([url], options)
);
}
}
}