-
-
Notifications
You must be signed in to change notification settings - Fork 241
Question; how to detect routerExtensions.back(); in the target Page? #617
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hi @devna13,
Another option is to register for page
Hope this helps. |
Perfect answer! @tsonevn 🥇 |
Look at my new post below for the updated solution. Original post:For those who want to specifically detect back navigation to a page can use this in target page component file: Import import { PlatformLocation } from '@angular/common'; Get service: constructor(private location : PlatformLocation){} Use it: ngOnInit() : void {
this.location.onPopState(() => {
// Do whatever you want
});
} |
|
Hi @jdnichollsc , |
@erkanarslan What about unregister it ? What is the right way to unsubscribe it ? ( and how) |
@erkanarslan, @RoyiNamir I'm also curious about that. But this isn't Observable/Promise object so I'm not sure if we should or have to unsubscribe that event. |
@tsonevn @RoyiNamir @divisble if you unsubscribe then how will it detect router events when coming back from other component? I have tons of routes if I subscribe to them all and don't unsubscribe doesn't seem right. Update: This is how I am unsubscribing:
|
IMHO this is somewhat of a limitation, particularly when we need to refresh data on a previous view on the stack. The ideal behaviour would be somewhat identical to what iOS's UIViewController provides: I'm currently trying to implement these hooks using a service that subscribes to router events, and uses the component instance reference attached to the |
@RoyiNamir @divisble To unsubscribe from this event, you can use Import library: import { Location } from '@angular/common'; Inject it: constructor(
private location : Location
) {} Subscribe to it: this.locationSub = this.location.subscribe(() => {
// do whatever you want here
}); Unsubscribe when page destroyed: ngOnDestroy() : void {
this.locationSub.unsubscribe();
} One thing to be careful is that when you navigate from page A to B, page A is not destroyed. It is pushed to navigation history. Because of that if you navigate like A -> B -> A -> C -> A and you subscribe to location on page A, you will have 3 different subscriptions each updating their own copy of the page component. If you are just updating the values in the component, this won't create a problem but if you are sending an http request, or calling a service it will be called 3 times. You can try to unsubscribe when navigated away from page A but in this case if you navigate back to page A, it won't do the operations that you do in subscribe method. |
Using
produces undesirable behavior as it is triggered on every pop. I found the following more suitable for this.
|
Is there a way to detect routerExtensions.back(); in the target page ?
I tried some of the component lifecycle hooks (constructor, ngOnInit, ngAfterContentInit, ngAfterViewInit, ....) but none is being called.
Thanks
The text was updated successfully, but these errors were encountered: