incorrect component passed to canDeactivate guard
See original GitHub issueπ bug report
Affected Package
The issue is caused by package @angular/routerDescription
For some reason CanDeactivate guard gets called with an incorrect component instance. In this case it should get called with a component that is mounted in a named outlet, but gets called with one from primary outlet. See example in stackblitz.
Iβd expect that with configuration like below:
The guard should only be called when AdminHeaderComponent is being deactivated and only with this component instance.
One notable thing is that the currentRoute param is correct!

EDIT: found a workaround:
canDeactivate(component: HasComponentPendingChanges, currentRoute: ActivatedRouteSnapshot, currentState: RouterStateSnapshot) {
if (currentRoute.outlet === OVERLAY_OUTLET && this.router['rootContexts'] && this.router['rootContexts'].getContext) {
const context: OutletContext = this.router['rootContexts'].getContext(OVERLAY_OUTLET);
if (context && context.outlet && context.outlet.isActivated && context.outlet['activated'].instance) {
component = context.outlet['activated'].instance;
}
}
π¬ Minimal Reproduction
https://stackblitz.com/edit/angular-named-outlet-yfgsip?file=app%2Fadmin%2Fadmin.module.ts
- Open the link
- Open the console
- Click on
adminlink - Click on
create userlink - Observe that following message is printed:
deactivating UsersListComponent {}
-
Now click on
hide navbarlink
-
Observe that following messages are printed:
deactivating CreateUserComponent {}
deactivating AdminHeaderComponent {}
^ all good
- Now the fun begins, click on
adminandcreate userlinks again - Click on
Component 2 - Observe that following messages are printed:
deactivating UsersListComponent {}
deactivating CreateUserComponent {}
deactivating CreateUserComponent {}
Iβd argue that the last message should state AdminHeaderComponent
π Your Environment
Angular Version:
8.2.14
Issue Analytics
- State:
- Created 4 years ago
- Reactions:7
- Comments:9 (5 by maintainers)

Top Related StackOverflow Question
Iβm also experiencing the same issues, but my
componentparam incanDeactivatereturns null instead of wrong component. This happens when my named outlet is present in parent route component template.I found this bug in version 10.0.2 but was able to reproduce in latest 11.0.1 https://stackblitz.com/edit/angular-ivy-efvmyh?file=src/app/can-deactivate-page.guard.ts
I believe my issue is related to the same as this
Expected
Console should display following messages: βcanDeactivate was calledβ (instance of
LazyComponent) βonPageDeactivation was calledβActual behaviour
Console only outputs: βcanDeactivate was calledβ (null)
Reopening - fix was reverted because it did not work with some configs.