Skip to content

Commit d4d3782

Browse files
elbywanmatsko
authored andcommitted
feat(Router): call resolver when upstream params change (angular#12942)
With this change the resolver is called when the parameter for the activated and any parent routes change. ie, switching from `/teams/10/players/5` to `/teams/12/players/5` will now trigger any `PlayerResolver`.
1 parent 46cb04d commit d4d3782

2 files changed

Lines changed: 51 additions & 4 deletions

File tree

modules/@angular/router/src/router_state.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,5 +356,9 @@ export function advanceActivatedRoute(route: ActivatedRoute): void {
356356

357357
export function equalParamsAndUrlSegments(
358358
a: ActivatedRouteSnapshot, b: ActivatedRouteSnapshot): boolean {
359-
return shallowEqual(a.params, b.params) && equalSegments(a.url, b.url);
360-
}
359+
const equalUrlParams = shallowEqual(a.params, b.params) && equalSegments(a.url, b.url);
360+
const parentsMismatch = !a.parent !== !b.parent;
361+
362+
return equalUrlParams && !parentsMismatch &&
363+
(!a.parent || equalParamsAndUrlSegments(a.parent, b.parent));
364+
}

modules/@angular/router/test/router_state.spec.ts

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,28 @@ describe('RouterState & Snapshot', () => {
100100

101101
describe('equalParamsAndUrlSegments', () => {
102102
function createSnapshot(params: Params, url: UrlSegment[]): ActivatedRouteSnapshot {
103-
return new ActivatedRouteSnapshot(
103+
const snapshot = new ActivatedRouteSnapshot(
104104
url, params, <any>null, <any>null, <any>null, <any>null, <any>null, <any>null, <any>null,
105105
-1, null);
106+
snapshot._routerState = new RouterStateSnapshot('', new TreeNode(snapshot, []));
107+
return snapshot;
108+
}
109+
110+
function createSnapshotPairWithParent(
111+
params: [Params, Params], parentParams: [Params, Params],
112+
urls: [string, string]): [ActivatedRouteSnapshot, ActivatedRouteSnapshot] {
113+
const snapshot1 = createSnapshot(params[0], []);
114+
const snapshot2 = createSnapshot(params[1], []);
115+
116+
const snapshot1Parent = createSnapshot(parentParams[0], [new UrlSegment(urls[0], {})]);
117+
const snapshot2Parent = createSnapshot(parentParams[1], [new UrlSegment(urls[1], {})]);
118+
119+
snapshot1._routerState =
120+
new RouterStateSnapshot('', new TreeNode(snapshot1Parent, [new TreeNode(snapshot1, [])]));
121+
snapshot2._routerState =
122+
new RouterStateSnapshot('', new TreeNode(snapshot2Parent, [new TreeNode(snapshot2, [])]));
123+
124+
return [snapshot1, snapshot2];
106125
}
107126

108127
it('should return false when params are different', () => {
@@ -123,6 +142,27 @@ describe('RouterState & Snapshot', () => {
123142
createSnapshot({a: 1}, [new UrlSegment('a', {})])))
124143
.toEqual(true);
125144
});
145+
146+
it('should return false when upstream params are different', () => {
147+
const [snapshot1, snapshot2] =
148+
createSnapshotPairWithParent([{a: 1}, {a: 1}], [{b: 1}, {c: 1}], ['a', 'a']);
149+
150+
expect(equalParamsAndUrlSegments(snapshot1, snapshot2)).toEqual(false);
151+
});
152+
153+
it('should return false when upstream urls are different', () => {
154+
const [snapshot1, snapshot2] =
155+
createSnapshotPairWithParent([{a: 1}, {a: 1}], [{b: 1}, {b: 1}], ['a', 'b']);
156+
157+
expect(equalParamsAndUrlSegments(snapshot1, snapshot2)).toEqual(false);
158+
});
159+
160+
it('should return true when upstream urls and params are equal', () => {
161+
const [snapshot1, snapshot2] =
162+
createSnapshotPairWithParent([{a: 1}, {a: 1}], [{b: 1}, {b: 1}], ['a', 'a']);
163+
164+
expect(equalParamsAndUrlSegments(snapshot1, snapshot2)).toEqual(true);
165+
});
126166
});
127167

128168
describe('advanceActivatedRoute', () => {
@@ -135,9 +175,12 @@ describe('RouterState & Snapshot', () => {
135175
const queryParams = {};
136176
const fragment = '';
137177
const data = {};
138-
return new ActivatedRouteSnapshot(
178+
const snapshot = new ActivatedRouteSnapshot(
139179
url, params, queryParams, fragment, data, <any>null, <any>null, <any>null, <any>null, -1,
140180
null);
181+
const state = new RouterStateSnapshot('', new TreeNode(snapshot, []));
182+
snapshot._routerState = state;
183+
return snapshot;
141184
}
142185

143186
it('should call change observers', () => {

0 commit comments

Comments
 (0)