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.

[Router 3] CanDeactivate target route

See original GitHub issue

I’m submitting a … (check one with “x”)

[ ] bug report
[x] 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 When routing is about to happen, CanDeactivate guards can only know about component instance about to be deactivated/reused along with current route state of the application.

Expected/desired behavior Add an extra parameter to CanDeactivate interface - something like nextUrl like so:

export interface CanDeactivate<T> {
    canDeactivate(component: T, route: ActivatedRouteSnapshot, state: RouterStateSnapshot, nextUrl: string): Observable<boolean> | boolean;
}

Using nextUrl instead of simple boolean value such as willReuse could help further and mimic the same functionality as deprecated router.

What is the motivation / use case for changing the behavior? This will help differentiate if the component will be deactivated or reused when the url changes. If the component is going to be destroyed, we can postpone the navigation as needed. This could be cleaning up some data (which will be unnecessary if the component is going to be reused and lifecycle hooks can be meaningless if this task runs on a service instead of component itself) or simply running some animations (which can be used page transitions; ngOnDestroy() is run after component is detached and it does not wait for any observable like CanDeactivate anyways).

Please tell us about your environment:

  • Angular version: 2.0.0-rc.4 - @angular/router 3.0.0-beta.2
  • Browser: [all ]
  • Language: [ TypeScript ]

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:21
  • Comments:21 (4 by maintainers)

github_iconTop GitHub Comments

15reactions
CoralSilvercommented, Jul 20, 2016

+1 for this feature

7reactions
jawachecommented, Oct 18, 2016

According to the documentation https://angular.io/docs/ts/latest/guide/router.html

The ActivatedRouteSnapshot contains the future route that will be activated and the RouterStateSnapshot contains the future RouterState of our application, should we pass through our guard check.

Right now it shows the current URL and not the future one so this should be a bug and not a feature request.

@vsavkin it seems that the other linked ticket https://github.com/angular/angular/issues/10186 with the bug report and the sample plunker is the correct one to keep open and fix isn’t it?

Read more comments on GitHub >

github_iconTop Results From Across the Web

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 >
Angular 2 RC5 get intended route in canDeactivate
Is it possible to get the intended route in the CanDeactivate guard in Angular 2 RC5 router? I see an answer to a...
Read more >
Router Guards • Angular - codecraft.tv
CanDeactivate. Checks to see if a user can exit a route. Resolve. Performs route data retrieval before route activation. CanLoad.
Read more >
Router tutorial: tour of heroes - Angular
The CanDeactivate guard (ask permission to discard unsaved changes). The Resolve guard (pre-fetching route data). Lazy loading an NgModule . The CanLoad guard...
Read more >
Chapter 4. Router advanced - Angular Development with ...
Listing 4.9. Adding CanDeactivate and CanDeactivate guards to a route. const routes: Routes = [ {path: '', component: HomeComponent} ...
Read more >

github_iconTop Related Medium Post

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