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.

CanDeactivate not triggered when navigating from lazy loaded module component to root module

See original GitHub issue

🐞 bug report

Affected Package

The issue is caused by package @angular/core, @angular/router.

Is this a regression?

I do not know precisely. There was maybe a similar issue described here: #16868

Description

When I put a CanDeactivate guard for the route of a component that is included inside a lazy loaded module and I try to navigate (for example with the back button) from this route to a route that is included in the root module (app.module.ts, for example the homepage) the CanDeactivate guard it is not called. This behaviour does not appear when navigating between two components (in which at least one is protected by the CanDeactivate) that are included in the same lazy-loaded module or are included in two different lazy-loaded modules.

πŸ”¬ Minimal Reproduction

https://stackblitz.com/edit/angular-issue-deactivate-guard-lazyload-module

πŸ”₯ Exception or Error

There are no exceptions or errors sadly…

🌍 Your Environment

Angular Version:




Angular CLI: 7.2.2
Node: 10.15.0
OS: darwin x64
Angular: 7.2.1
... animations, cdk, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.12.2
@angular-devkit/build-angular     0.12.2
@angular-devkit/build-optimizer   0.12.2
@angular-devkit/build-webpack     0.12.2
@angular-devkit/core              7.2.2
@angular-devkit/schematics        7.2.2
@angular/cli                      7.2.2
@angular/flex-layout              7.0.0-beta.23
@angular/http                     7.2.2
@ngtools/webpack                  7.2.2
@schematics/angular               7.2.2
@schematics/update                0.12.2
rxjs                              6.3.3
typescript                        3.2.2
webpack                           4.28.4

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:15
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
narendrasinghrathorecommented, Feb 22, 2021
ERROR Error: Uncaught (in promise): TypeError: component.confirmAndNavigate is not a function

When returning from lazy loaded module to root module.

Root Module

Lazy Loaded Feature Module

  • Child component having unsaved changes, trigger canDeacticvate but provider don’t have component reference

  • Child lazy loaded modules having single components with canDeactivate guard working fine in between component navigation

Working ( with canDeactivate guard )

  • /root-route/lazy-loaded-parent/lazy-loaded-child1 to /root-route/lazy-loaded-parent/lazy-loaded-child2

Not working ( with canDeactivate guard )

  • From /root-route/lazy-oaded-parent/lazy-loaded-child-xx to /root-route
0reactions
angular-automatic-lock-bot[bot]commented, Jul 28, 2022

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

canDeactivate() is not triggered on page navigation in Angular 8
Maybe the problem is that you r trying to deactivate a set of lazy loaded routes. Try to apply the CanDeactivateGuard to the...
Read more >
CanDeactivate - Angular
Interface that a class can implement to be a guard deciding if a route can be deactivated. If all guards return true ,...
Read more >
Resolving errors faced while implementing Lazy Loading with ...
HelloComponent may show as the root component even though it is not declared at the root route of AppComponent. Check that the imported...
Read more >
In-app navigation: routing to views - Angular
The following command uses the Angular CLI to generate a basic Angular app with an app routing module, called AppRoutingModule , which is...
Read more >
Understanding Angular Guards - codeburst
Modules in angular can be loaded all at once or be lazy loaded . ... which gets called whenever the user tries to...
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