OnDestroy doesn't work for angular navigation lifecycle hooks. That's fine, but i still need to detect the page leaving event.
What's the best way to do that?
Try the page unloaded event.
On 20 Oct 2017 5:10 p.m., "morzyns" notifications@github.com wrote:
OnDestroy doesn't work for angular navigation lifecycle hooks. That's fine,
but i still need to detect the page leaving event.
What's the best way to do that?
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/NativeScript/nativescript-angular/issues/1049, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AE2PtppY8ySP0b2Bt6_KuK3FTvN2cyDwks5suIahgaJpZM4QAjO8
.
Here is how I handle it:
constructor(private page: Page) {}
ngOnInit(): void {
this.page.on('navigatingTo', (data) => {
// run init code
// (note: this will run when you either move forward or back to this page)
}
this.page.on('navigatingFrom', (data) => {
// run destroy code
// (note: this will run when you either move forward to a new page or back to the previous page)
}
}
You could probably try this in the constructor also if you do not need an OnInit in the controller where you need this, but I usually add this to the OnInit.
Thanks @jfturcot, i tried similar approach but with a router navigatingFrom and navigatingTo events. in my case, navigatingFrom & navigatingTo were hit every time simultaneously.
Hi @morzyns,
Did you try the solution provided by @anuragd7 to handle page unloaded event? For example:
this.page.on('unloaded ', (data) => {
// run init code
// (note: this will run when you either move forward or back to this page)
}
A bit late to the party, but since the OP is asking about Angular, I wanted to point out that if you are not using page navigation, the page navigation events mentioned above will of course not fire. In that case you you can listen to Angular's navigation events like below:
import { NavigationStart, Router } from '@angular/router';
private routeSub:any; // subscription to route observer
public ngOnInit() {
/*
Register to Angular navigation events to detect navigating away (so we can save changed settings for example)
*/
this.routeSub = this.router.events.subscribe((event) => {
if (event instanceof NavigationStart) {
// save your data
}
});
}
public ngOnDestroy() {
this.routeSub.unsubscribe();
}
@abhayastudios - in the code you posted here, you are unsubscribing in ngOnDestroy. NgOnDestroy won't necessarily fire when you navigate away from the angular component, since NativeScript caches the component.
This means that the subscription will exist beyond the point when you navigate away from the component and you'll be executing the subscription callback on NavigationStart events of other components.
where do you import { Page } from ? Sorry I'm new to NativeScript and I see here that it is ui/page but importing from nativescript-angular/ui/page doesn't work
EDIT: ah, for anyone else new around here arriving from google
import { Page } from 'ui/page';
@CoreyCole
import { Page } from 'tns-core-modules/ui/page';
Use
page.on(Page.navigatingFromEvent, () => {
this.ngOnDestroy(); // Replace by what you want
});
And not Page.unloadedEvent because Page.unloadedEvent is also triggered when the application goes into background.
I use angular 7 lifecycle hooks:
import { Component, OnDestroy } from '@angular/core';
export class SampleComponent implements OnDestroy {
ngOnDestroy() {
// code here
}
}
I wrote an article about this. I solved it in generic way with the decorator.
@joelau71 Unfortunately ngOnDestroy isn't called when you navigate to a new page
@Stanteq Nice solution! This will avoid some duplication, especially with a NativeScript + Web application. It's ugly to inject the injector, but if we have no choice...
Instead of injecting the injector in each page, we could store the injector in a static var ... It's ugly too but it's ugly in only one place! https://stackoverflow.com/questions/39409328/storing-injector-instance-for-use-in-components
@Stanteq
This solution would not work for modals
@Stanteq
This solution would not work for modals
@juniorschen, As I know the modals works well without this workaround.
@juniorschen how so? ngOnDestroy is called in modals without navigation and page navigation works normally with named outlets
@edusperoni I didn't know that in modals destroy was usually called hehe
@edusperoni
In navigation modals, will I have the page instance to register for the event?
@juniorschen if you don't have a Page, ngOnDestroy is called normally, so you don't need this workaround at all
@edusperoni
Ah vlw irmão, ajudou muito a explicação
Most helpful comment
Here is how I handle it:
You could probably try this in the constructor also if you do not need an OnInit in the controller where you need this, but I usually add this to the OnInit.