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.

fix(router): canDeactivate should not change the url when returns false and skipLocationChange has been set to true

See original GitHub issue

I’m submitting a … (check one with β€œx”)

[X] bug report => search github for a similar issue or PR before submitting
[ ] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior This is leftover from : https://github.com/angular/angular/issues/10321

With the following routes:

path: 'view',
children: [
    {

        path: ':id',
        component: CustomerViewComponent,
        resolve: {
            customerViewData: CustomerResolve,
        },
        canDeactivate: [CanDeactivateGuard],
        children: [
            {
                path: 'edit',
            },
            {
                path: ''
            }
        ]
    }
]

If you navigate to a route like this using skipLocationChange:

this.router.navigate(['edit'], { relativeTo: this.route, skipLocationChange: true });

Then when you are about to navigate away and return false in canDeactivate(), the /edit creeps in the URL.

Expected behavior The URL should not change to /edit

Minimal reproduction of the problem with instructions

http://plnkr.co/edit/K1JnuZfQ5S0WHnWmp6Zv?p=preview

Steps:

  • pop out the preview window to see the address bar
  • click on a crisis center
  • click on β€œDragon burning cities”
  • The URL stays /crisis-center because of skipLocationChange
  • and edit the text
  • use the browser back commands to go back.

ACTUAL:
The URL goes to /crisis-center/1

EXPECTED:
The URL should stay to /crisis-center

What is the motivation / use case for changing the behavior? This causes further problem when using relative routing such as …/…

Please tell us about your environment: Visual Studio 2015, IIS Express, npm@3.10.5

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:5
  • Comments:16 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
vanradocommented, Jun 27, 2017

As I see, still not fixed in 4.2.4 😦

2reactions
rsirscommented, Feb 22, 2018
came across similar problem, a simple working around I have used

canActivate(route, state) {
  /* problem is with history */
   if(activate == false) 
      this.platformLocation.pushState('state', 'msg', previousVisitedRoute);
   return activate;
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

angular2 routing - CanDeactivate changing the window history
Here is the issue, but it still isn't fixed. As a workaround, you can manually put the active url back to the history:...
Read more >
CanDeactivate Guard in Angular Router (2021) - YouTube
Before I start to explaining CanDeactivate Router Guard in Angular I would like to ask you if you have ever had a situation...
Read more >
NavigationExtras - Angular
Supply an object containing any of these properties to a Router navigation function to control how the target URL should be constructed or...
Read more >
Ninja Squad: Le Blog
The same kind of thing has been done with the RouterLink directive in ... Note that a CanMatch guard that returns false does...
Read more >
https://unpkg.com/@angular/router@12.2.1/router.d.ts
This strategy does not store any routes for later reuse. ... This strategy returns `true` when the future route config and current route...
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