question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

incorrect component passed to canDeactivate guard

See original GitHub issue

🐞 bug report

Affected Package

The issue is caused by package @angular/router

Description

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: image 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! image

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 admin link
  • Click on create user link
  • Observe that following message is printed:
deactivating UsersListComponent {}
  • Now click on hide navbar link image

  • Observe that following messages are printed:

deactivating CreateUserComponent {}
deactivating AdminHeaderComponent {}

^ all good

  • Now the fun begins, click on admin and create user links 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:open
  • Created 4 years ago
  • Reactions:7
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
krilllindcommented, Nov 19, 2020

I’m also experiencing the same issues, but my component param in canDeactivate returns 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)

1reaction
atscottcommented, Apr 22, 2020

Reopening - fix was reverted because it did not work with some configs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting properties from wrong component in ... - Stack Overflow
If you want to see your form component on the browser console, you need to pass it. component: FormComponent, canDeactivate: [CanDeactivateGuard]Β ...
Read more >
Angular | canDeactivate Guard - Medium
The guard can be added to any component route using canDeactivate ... Here, we are passing a true value to isNavigationAllowed method toΒ ......
Read more >
CanDeactivate - Angular
If any guard returns false , navigation is cancelled. ... interface CanDeactivate<T> { canDeactivate(component: T, currentRoute: ActivatedRouteSnapshot,Β ...
Read more >
Controlling Access to or from a Route - Rangle.io
We can use route guards to check these conditions and control access to routes. ... The canDeactivate function passes the component being deactivated...
Read more >
Controlling Navigation with CanDeactivate Guard
The CanDeactivate Guard to prevent the user from accidentally navigating away. ... export class Component implements OnInt{ changesSaved = false;Β ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found