feat(router): restore whole object when navigating back to a page managed by Angular router (#27198)

PR Close #27198
This commit is contained in:
Jason Aden
2018-11-14 15:24:40 -08:00
committed by Igor Minar
parent 1b84b11cf5
commit 26842491c6

View File

@ -181,7 +181,7 @@ export type NavigationTransition = {
reject: any, reject: any,
promise: Promise<boolean>, promise: Promise<boolean>,
source: NavigationTrigger, source: NavigationTrigger,
state: {navigationId: number} | null, restoredState: {navigationId: number} | null,
currentSnapshot: RouterStateSnapshot, currentSnapshot: RouterStateSnapshot,
targetSnapshot: RouterStateSnapshot | null, targetSnapshot: RouterStateSnapshot | null,
currentRouterState: RouterState, currentRouterState: RouterState,
@ -351,7 +351,7 @@ export class Router {
reject: null, reject: null,
promise: Promise.resolve(true), promise: Promise.resolve(true),
source: 'imperative', source: 'imperative',
state: null, restoredState: null,
currentSnapshot: this.routerState.snapshot, currentSnapshot: this.routerState.snapshot,
targetSnapshot: null, targetSnapshot: null,
currentRouterState: this.routerState, currentRouterState: this.routerState,
@ -393,7 +393,7 @@ export class Router {
switchMap(t => { switchMap(t => {
const transition = this.transitions.getValue(); const transition = this.transitions.getValue();
eventsSubject.next(new NavigationStart( eventsSubject.next(new NavigationStart(
t.id, this.serializeUrl(t.extractedUrl), t.source, t.state)); t.id, this.serializeUrl(t.extractedUrl), t.source, t.restoredState));
if (transition !== this.transitions.getValue()) { if (transition !== this.transitions.getValue()) {
return EMPTY; return EMPTY;
} }
@ -431,9 +431,9 @@ export class Router {
* handle this "error condition" by navigating to the previously successful URL, * handle this "error condition" by navigating to the previously successful URL,
* but leaving the URL intact.*/ * but leaving the URL intact.*/
if (processPreviousUrl) { if (processPreviousUrl) {
const {id, extractedUrl, source, state, extras} = t; const {id, extractedUrl, source, restoredState, extras} = t;
const navStart = const navStart = new NavigationStart(
new NavigationStart(id, this.serializeUrl(extractedUrl), source, state); id, this.serializeUrl(extractedUrl), source, restoredState);
eventsSubject.next(navStart); eventsSubject.next(navStart);
const targetSnapshot = const targetSnapshot =
createEmptyState(extractedUrl, this.rootComponentType).snapshot; createEmptyState(extractedUrl, this.rootComponentType).snapshot;
@ -681,9 +681,9 @@ export class Router {
this.locationSubscription = <any>this.location.subscribe((change: any) => { this.locationSubscription = <any>this.location.subscribe((change: any) => {
let rawUrlTree = this.parseUrl(change['url']); let rawUrlTree = this.parseUrl(change['url']);
const source: NavigationTrigger = change['type'] === 'popstate' ? 'popstate' : 'hashchange'; const source: NavigationTrigger = change['type'] === 'popstate' ? 'popstate' : 'hashchange';
const state = change.state && change.state.navigationId ? // Navigations coming from Angular router have a navigationId state property. When this
{navigationId: change.state.navigationId} : // exists, restore the state.
null; const state = change.state && change.state.navigationId ? change.state : null;
setTimeout( setTimeout(
() => { this.scheduleNavigation(rawUrlTree, source, state, {replaceUrl: true}); }, 0); () => { this.scheduleNavigation(rawUrlTree, source, state, {replaceUrl: true}); }, 0);
}); });
@ -917,7 +917,7 @@ export class Router {
} }
private scheduleNavigation( private scheduleNavigation(
rawUrl: UrlTree, source: NavigationTrigger, state: {navigationId: number}|null, rawUrl: UrlTree, source: NavigationTrigger, restoredState: {navigationId: number}|null,
extras: NavigationExtras): Promise<boolean> { extras: NavigationExtras): Promise<boolean> {
const lastNavigation = this.getTransition(); const lastNavigation = this.getTransition();
// If the user triggers a navigation imperatively (e.g., by using navigateByUrl), // If the user triggers a navigation imperatively (e.g., by using navigateByUrl),
@ -955,7 +955,7 @@ export class Router {
this.setTransition({ this.setTransition({
id, id,
source, source,
state, restoredState,
currentUrlTree: this.currentUrlTree, currentUrlTree: this.currentUrlTree,
currentRawUrl: this.rawUrlTree, rawUrl, extras, resolve, reject, promise, currentRawUrl: this.rawUrlTree, rawUrl, extras, resolve, reject, promise,
currentSnapshot: this.routerState.snapshot, currentSnapshot: this.routerState.snapshot,