diff --git a/packages/router/src/utils/preactivation.ts b/packages/router/src/utils/preactivation.ts index 0334810748..09e87c8ef5 100644 --- a/packages/router/src/utils/preactivation.ts +++ b/packages/router/src/utils/preactivation.ts @@ -121,9 +121,8 @@ function getRouteGuards( getChildRouteGuards(futureNode, currNode, parentContexts, futurePath, checks); } - if (shouldRun) { - const component = context && context.outlet && context.outlet.component || null; - checks.canDeactivateChecks.push(new CanDeactivate(component, curr)); + if (shouldRun && context && context.outlet && context.outlet.isActivated) { + checks.canDeactivateChecks.push(new CanDeactivate(context.outlet.component, curr)); } } else { if (curr) { diff --git a/packages/router/test/integration.spec.ts b/packages/router/test/integration.spec.ts index 580360f333..6c5e5e5d89 100644 --- a/packages/router/test/integration.spec.ts +++ b/packages/router/test/integration.spec.ts @@ -3027,6 +3027,13 @@ describe('Integration', () => { resolve: {data: 'resolver'}, }, ] + }, + { + path: 'throwing', + runGuardsAndResolvers, + component: ThrowingCmp, + canActivate: ['guard'], + resolve: {data: 'resolver'} } ]); @@ -3125,6 +3132,15 @@ describe('Integration', () => { advance(fixture); expect(guardRunCount).toEqual(5); expect(recordedData).toEqual([{data: 0}, {data: 1}, {data: 2}, {data: 3}, {data: 4}]); + + // Issue #39030, always running guards and resolvers should not throw + // when navigating away from a component with a throwing constructor. + expect(() => { + router.navigateByUrl('/throwing').catch(() => {}); + advance(fixture); + router.navigateByUrl('/a;p=1'); + advance(fixture); + }).not.toThrow(); }))); it('should rerun rerun guards and resolvers when path params change',