feat(router): add pathParamsChange mode for runGuardsAndResolvers (#26861)
This option means guards and resolvers will ignore changes to optional parameters such as query and matrix params. When the path or any path params change, guards and resolvers will be run Related to discussion in #18253 FW-560 #resolve PR Close #26861
This commit is contained in:

committed by
Andrew Kushnir

parent
3da82338d1
commit
bf6ac6cef8
@ -2138,7 +2138,13 @@ describe('Integration', () => {
|
||||
canActivate: ['guard'],
|
||||
resolve: {data: 'resolver'}
|
||||
},
|
||||
{path: 'b', component: SimpleCmp, outlet: 'right'}
|
||||
{path: 'b', component: SimpleCmp, outlet: 'right'}, {
|
||||
path: 'c/:param',
|
||||
runGuardsAndResolvers,
|
||||
component: RouteCmp,
|
||||
canActivate: ['guard'],
|
||||
resolve: {data: 'resolver'}
|
||||
}
|
||||
]);
|
||||
|
||||
router.navigateByUrl('/a');
|
||||
@ -2236,6 +2242,55 @@ describe('Integration', () => {
|
||||
expect(guardRunCount).toEqual(5);
|
||||
expect(recordedData).toEqual([{data: 0}, {data: 1}, {data: 2}, {data: 3}, {data: 4}]);
|
||||
})));
|
||||
|
||||
it('should not rerun guards and resolvers', fakeAsync(inject([Router], (router: Router) => {
|
||||
const fixture = configureRouter(router, 'pathParamsChange');
|
||||
|
||||
const cmp: RouteCmp = fixture.debugElement.children[1].componentInstance;
|
||||
const recordedData: any[] = [];
|
||||
cmp.route.data.subscribe((data: any) => recordedData.push(data));
|
||||
|
||||
// First navigation has already run
|
||||
expect(guardRunCount).toEqual(1);
|
||||
expect(recordedData).toEqual([{data: 0}]);
|
||||
|
||||
// Changing any optional params will not result in running guards or resolvers
|
||||
router.navigateByUrl('/a;p=1');
|
||||
advance(fixture);
|
||||
expect(guardRunCount).toEqual(1);
|
||||
expect(recordedData).toEqual([{data: 0}]);
|
||||
|
||||
router.navigateByUrl('/a;p=2');
|
||||
advance(fixture);
|
||||
expect(guardRunCount).toEqual(1);
|
||||
expect(recordedData).toEqual([{data: 0}]);
|
||||
|
||||
router.navigateByUrl('/a;p=2?q=1');
|
||||
advance(fixture);
|
||||
expect(guardRunCount).toEqual(1);
|
||||
expect(recordedData).toEqual([{data: 0}]);
|
||||
|
||||
router.navigateByUrl('/a;p=2(right:b)?q=1');
|
||||
advance(fixture);
|
||||
expect(guardRunCount).toEqual(1);
|
||||
expect(recordedData).toEqual([{data: 0}]);
|
||||
|
||||
// Change to new route with path param should run guards and resolvers
|
||||
router.navigateByUrl('/c/paramValue');
|
||||
advance(fixture);
|
||||
|
||||
expect(guardRunCount).toEqual(2);
|
||||
|
||||
// Modifying a path param should run guards and resolvers
|
||||
router.navigateByUrl('/c/paramValueChanged');
|
||||
advance(fixture);
|
||||
expect(guardRunCount).toEqual(3);
|
||||
|
||||
// Adding optional params should not cause guards/resolvers to run
|
||||
router.navigateByUrl('/c/paramValueChanged;p=1?q=2');
|
||||
advance(fixture);
|
||||
expect(guardRunCount).toEqual(3);
|
||||
})));
|
||||
});
|
||||
|
||||
describe('should wait for parent to complete', () => {
|
||||
|
Reference in New Issue
Block a user