Repository navigation
Description
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
CoralSilver commentedon Jul 20, 2016
+1 for this feature
vinzandtonz commentedon Jul 20, 2016
+1 for this feature
brandonroberts commentedon Jul 20, 2016
If the
RouterStatewas the proposed router state then we could access it throughRouterState.urljawache commentedon Oct 18, 2016
According to the documentation https://angular.io/docs/ts/latest/guide/router.html
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?
AKArchy commentedon Nov 2, 2016
+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.
PEsteves8 commentedon Nov 3, 2016
+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.
PEsteves8 commentedon Dec 2, 2016
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)
strpipe commentedon Dec 8, 2016
+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.DzmitryShylovich commentedon Dec 8, 2016
console.logwill show the object at the last state of execution, not at the state whenconsole.logwas called.strpipe commentedon Dec 8, 2016
Okay. Can you please tell me how to access the future router state?
feat(router): add an extra argument to CanDeactivate interface (#13560)
kblestarge commentedon Feb 1, 2017
I'm trying to use the nextState snapshot, but it is always undefined. Any suggestions?
DzmitryShylovich commentedon Feb 1, 2017
Pls provide a reproduction
killface commentedon Mar 13, 2017
@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.
amitgaur208 commentedon Apr 18, 2017
@AKArchy Have you find any way to redirect on next state after custom pop-up.
amitgaur208 commentedon Apr 18, 2017
@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.
AKArchy commentedon Apr 23, 2017
@amitgaur208 - This would be how to do it...
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.
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.
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.
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.
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.
CaselIT commentedon Apr 23, 2017
@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.
sureshyalamanchi commentedon Nov 2, 2017
geocine commentedon Jun 12, 2018
@killface any workaround for angular2+ ?
redwolf2 commentedon Jan 23, 2019
Crappy workaround:
This is just occam's razor applied to the approach of @AKArchy, which may be more sophisticated, depending on the task at hand.
angular-automatic-lock-bot commentedon Sep 14, 2019
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.