Skip to content

[Router 3] CanDeactivate target route #9853

Closed

Description

@kgurkan

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 ]

Activity

  1. CoralSilver commented on Jul 20, 2016

    @CoralSilver

    +1 for this feature

  2. vinzandtonz commented on Jul 20, 2016

    @vinzandtonz

    +1 for this feature

  3. brandonroberts commented on Jul 20, 2016

    @brandonroberts
    Contributor

    If the RouterState was the proposed router state then we could access it through RouterState.url

  4. added
    feature
    on Jul 22, 2016
  5. jawache commented on Oct 18, 2016

    @jawache

    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 #10186 with the bug report and the sample plunker is the correct one to keep open and fix isn't it?

  6. AKArchy commented on Nov 2, 2016

    @AKArchy

    +1 for this.

    In my use case, I'm trying preventing navigation on a form when changes are unsaved. I don't want to use the simple "window.confirm()" function because this gives only two options to the user (OK or Cancel). Instead, I want the user to have the options to save or discard changes, which then navigates to the intended route after an async action. This requires that the router guard passes the intended route to another component/service and returns false.

    Since this isn't possible in CanDeactivate, I have had to use CanActivate guard service on all routes to prevent navigation. It would be more optimal to add the guard to just the route that needs it.

  7. PEsteves8 commented on Nov 3, 2016

    @PEsteves8

    +1

    Yeah I have been spending some time trying to make something similar work, until I found this issue.

    In my case, I want to make sure that the guard only does the check if the component actually changes. I frequently only change optional route params. That would mean that every time an optional route param changes, I would ask the users if they're sure they want to discard changes. I'm not seeing a clean way of synchronously returning true if there isn't an actual component change.

    For now, I'll just always allow navigation, until I can make it work.

  8. PEsteves8 commented on Dec 2, 2016

    @PEsteves8

    Any news on this? I find it weird that the frequency of this would be low. (Of course, I could just be biased for being directly affected by it, though)

  9. strpipe commented on Dec 8, 2016

    @strpipe

    +1

    console.log(this.router); this will log me the router object and I can clearly see that is knows the future url.
    However, if I do console.log(this.router.url); It will log me the current url. Seems like a real bug to me.

  10. DzmitryShylovich commented on Dec 8, 2016

    @DzmitryShylovich
    Contributor

    console.log will show the object at the last state of execution, not at the state when console.log was called.

  11. strpipe commented on Dec 8, 2016

    @strpipe

    Okay. Can you please tell me how to access the future router state?

  12. added a commit that references this issue on Dec 27, 2016
    69fa3bb
  13. kblestarge commented on Feb 1, 2017

    @kblestarge

    I'm trying to use the nextState snapshot, but it is always undefined. Any suggestions?

  14. DzmitryShylovich commented on Feb 1, 2017

    @DzmitryShylovich
    Contributor

    Pls provide a reproduction

  15. killface commented on Mar 13, 2017

    @killface

    @kblestarge Looks like this was added to Angular 4.0.0-beta.2, so if you're still on Angular 2.* then nextState will always be undefined.

  16. amitgaur208 commented on Apr 18, 2017

    @amitgaur208

    @AKArchy Have you find any way to redirect on next state after custom pop-up.

  17. amitgaur208 commented on Apr 18, 2017

    @amitgaur208

    @DzmitryShylovich Now i can get nextSatate Url in guard itself but how can i pass it to component to navigate after clicking on custom model pop-up button. Please help.

  18. AKArchy commented on Apr 23, 2017

    @AKArchy

    @amitgaur208 - This would be how to do it...

    1. Create a modal component that can be activated by another component/service. It depends on the design, but you'd probably want this on the app/root component so it can cover the whole app. You could have it show up by adding a class to the app/root level DOM element, or by toggling a flag on the app/root component hooked into *ngIf.

    2. Create a way to store the nextState (the future route). This could be a public function/variable on the modal component or the app component, or perhaps its own injectable service. The key is that it needs to be accessible to the modal component.

    3. Create a function that evaluates when to show the modal and returns true/false. For example, the function might check for changes on a form, then return false if no changes, or true if changes. This could be on the component you want to block navigation from, or its own service, depending on your design needs.

    4. Create a service that implements CanDeactivate for the component/route you want to prevent navigation from. Hook this into the route. Inside the canDeactivate function, have it call the function that evaluates when to show the modal. If the modal should be shown, the canDeactivate function should store the nextState in the function created for that purpose in Step 2, activate the modal component from Step 1, and return false to prevent navigation.

    5. When the modal component is activated, have it also check the component or service where the nextState object was set. Then the modal component can update the click events on its buttons to point the router to the "url" in the nextState.

  19. CaselIT commented on Apr 23, 2017

    @CaselIT
    Contributor

    @amitgaur208 Why don't just return a Promise or an Obserbable in the guard that resolves to true or false depending on the user action? Then have the pop-up resolve the promise/obserbable when the user clicks the appropriate button.

  20. sureshyalamanchi commented on Nov 2, 2017

    @sureshyalamanchi
    • 1
  21. geocine commented on Jun 12, 2018

    @geocine

    @killface any workaround for angular2+ ?

  22. redwolf2 commented on Jan 23, 2019

    @redwolf2

    Crappy workaround:

    1. Add a boolean flag overrideCanDeactivateOnce to a service.
    2. Check if it is true inside canDeactivate, then set it to false and return true.
    3. Before navigation, set the overrideCanDeactivateOnce to true.

    This is just occam's razor applied to the approach of @AKArchy, which may be more sophisticated, depending on the task at hand.

  23. angular-automatic-lock-bot commented on Sep 14, 2019

    @angular-automatic-lock-bot

    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.

  24. locked and limited conversation to collaborators on Sep 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: routerfeaturefreq1: low

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions