Skip to content

Commit e20cfe1

Browse files
roylingalxhub
authored andcommitted
fix(router): canDeactivate guards should run from bottom to top
Closes #15657.
1 parent eb6fb5f commit e20cfe1

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

packages/router/src/router.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -839,11 +839,10 @@ export class PreActivation {
839839

840840
// reusing the node
841841
if (curr && future._routeConfig === curr._routeConfig) {
842-
if (this.shouldRunGuardsAndResolvers(
843-
curr, future, future._routeConfig !.runGuardsAndResolvers)) {
842+
const shouldRunGuardsAndResolvers = this.shouldRunGuardsAndResolvers(
843+
curr, future, future._routeConfig !.runGuardsAndResolvers);
844+
if (shouldRunGuardsAndResolvers) {
844845
this.canActivateChecks.push(new CanActivate(futurePath));
845-
const outlet = context !.outlet !;
846-
this.canDeactivateChecks.push(new CanDeactivate(outlet.component, curr));
847846
} else {
848847
// we need to set the data
849848
future.data = curr.data;
@@ -859,6 +858,11 @@ export class PreActivation {
859858
} else {
860859
this.traverseChildRoutes(futureNode, currNode, parentContexts, futurePath);
861860
}
861+
862+
if (shouldRunGuardsAndResolvers) {
863+
const outlet = context !.outlet !;
864+
this.canDeactivateChecks.push(new CanDeactivate(outlet.component, curr));
865+
}
862866
} else {
863867
if (curr) {
864868
this.deactivateRouteAndItsChildren(currNode, context);

packages/router/test/integration.spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2435,6 +2435,11 @@ describe('Integration', () => {
24352435
provide: 'canDeactivate_team',
24362436
useFactory: (logger: Logger) => () => (logger.add('canDeactivate_team'), true),
24372437
deps: [Logger]
2438+
},
2439+
{
2440+
provide: 'canDeactivate_simple',
2441+
useFactory: (logger: Logger) => () => (logger.add('canDeactivate_simple'), true),
2442+
deps: [Logger]
24382443
}
24392444
]
24402445
});
@@ -2468,6 +2473,31 @@ describe('Integration', () => {
24682473
'canDeactivate_team', 'canActivateChild_parent', 'canActivate_team'
24692474
]);
24702475
})));
2476+
2477+
it('should call deactivate guards from bottom to top',
2478+
fakeAsync(inject(
2479+
[Router, Location, Logger], (router: Router, location: Location, logger: Logger) => {
2480+
const fixture = createRoot(router, RootCmp);
2481+
2482+
router.resetConfig([{
2483+
path: '',
2484+
children: [{
2485+
path: 'team/:id',
2486+
canDeactivate: ['canDeactivate_team'],
2487+
children:
2488+
[{path: '', component: SimpleCmp, canDeactivate: ['canDeactivate_simple']}],
2489+
component: TeamCmp
2490+
}]
2491+
}]);
2492+
2493+
router.navigateByUrl('/team/22');
2494+
advance(fixture);
2495+
2496+
router.navigateByUrl('/team/33');
2497+
advance(fixture);
2498+
2499+
expect(logger.logs).toEqual(['canDeactivate_simple', 'canDeactivate_team']);
2500+
})));
24712501
});
24722502
});
24732503

0 commit comments

Comments
 (0)