-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(RouterStore): change the default serializer to work around cycles…
… in RouterStateSnapshot
- Loading branch information
1 parent
a0f45ff
commit 7917a27
Showing
1 changed file
with
39 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,47 @@ | ||
import { InjectionToken } from '@angular/core'; | ||
import { RouterStateSnapshot } from '@angular/router'; | ||
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router'; | ||
|
||
export abstract class RouterStateSerializer<T> { | ||
abstract serialize(routerState: RouterStateSnapshot): T; | ||
} | ||
|
||
export interface SerializedRouterStateSnapshot { | ||
root: ActivatedRouteSnapshot; | ||
url: string; | ||
} | ||
|
||
export class DefaultRouterStateSerializer | ||
implements RouterStateSerializer<RouterStateSnapshot> { | ||
serialize(routerState: RouterStateSnapshot) { | ||
return routerState; | ||
implements RouterStateSerializer<SerializedRouterStateSnapshot> { | ||
serialize(routerState: RouterStateSnapshot): SerializedRouterStateSnapshot { | ||
return { | ||
root: this.serializeRoute(routerState.root), | ||
url: routerState.url, | ||
}; | ||
} | ||
|
||
private serializeRoute( | ||
route: ActivatedRouteSnapshot | ||
): ActivatedRouteSnapshot { | ||
const children = route.children.map(c => this.serializeRoute(c)); | ||
return { | ||
params: route.params, | ||
paramMap: route.paramMap, | ||
data: route.data, | ||
url: route.url, | ||
outlet: route.outlet, | ||
routeConfig: { | ||
component: route.routeConfig ? route.routeConfig.component : undefined, | ||
}, | ||
queryParams: route.queryParams, | ||
queryParamMap: route.queryParamMap, | ||
fragment: route.fragment, | ||
component: (route.routeConfig | ||
? route.routeConfig.component | ||
: undefined) as any, | ||
root: undefined as any, | ||
parent: undefined as any, | ||
firstChild: children[0], | ||
pathFromRoot: undefined as any, | ||
children, | ||
}; | ||
} | ||
} |