-
-
Notifications
You must be signed in to change notification settings - Fork 241
Component lifecycles differs on nativescript-angular and angular2 on web. #374
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
@m-abs You are indeed correct. When doing a page navigation in native app the page/component you are navigation from is not actually destroyed. When you navigate back to it the component is not created again. The original component is shown instead. Can you please elaborate on you scenario and why this is a problem? There are also a couple of other ways to plug into the app navigation:
|
@vakrilov I'll can try to elaborate, sorry for the long text :) Our project is based on Since the page-component lives on after navigation from it, the subscriptions and events created in I also think the behavior goes against what the ng2-docs imply. From: https://angular.io/docs/ts/latest/guide/lifecycle-hooks.html#!#hooks-overview
From: https://angular.io/docs/ts/latest/guide/router.html#!#route-parameters
We can of cause use the I can see it’s done intentionally here: https://github.com/NativeScript/nativescript-angular/blob/master/nativescript-angular/router/page-router-outlet.ts#L112 |
+1 |
Maybe some intermediate approach is when using the paged navigation indicate which is the expected reusability of the components (I would say destroy as default and preserve as alternative). |
Any updates on this ? Very urgent! |
We are using it in a soon to be launched app and we have already invested too much time with nativescript to go back now. This issue is going to be a dealbreaker. I am willing to help if someone can let me know what do do. |
Our designer wants a three tabbed interface, where the navbar is always visible at the bottom of the screen and the user can tap on each tab. I've been considering using an JS-hack to work around this by making the components constructor return the first component, but this is not a solution I like. @Component({...})
export class PageComponent {
private static singleton: PageComponent;
constructor(...) {
if (PageComponent.singleton) {
return PageComponent.singleton;
}
PageComponent.singleton = this;
}
} There're a bunch of problems with this solution, like potentially breaking DI, since the first components injections will always be used. and ngOnInit would get called multiple times. I've also considered making a private fork of the ns-router that doesn't have caching feature, but I really don't want to maintain it. |
Any way to temporarily disable cache folks ? This issue is really the breaking point and we need to launch our app ASAP. Can someone please help out. Thanks in advance |
@sivsushruth |
@sivsushruth |
@m-abs The issue however is, I need caching for a few pages, is it possible to have router-outlet inside page-router-outlet ? |
@sivsushruth We've created a few child routes like this: https://github.com/m-abs/angular2-seed-advanced/blob/child-router-android-back-button/src/client/app/components/home/home.routes.ts Routes under |
+1 for ng2 lifecycle or additional hooks to determine cache state of page. This would be helpful to subscribe / desubscribe from observables (e.g. if you navigate by observable return on pages, you get hit by race conditions to which page you actually will be navigated). |
@tviel You can listen to router.events, you can't use ActivatedRoute, because it seems to stop emitting events after first navigate away from the component... Could we please make this behavior optional? It makes sense to cache some pages but not others. |
@tviel Update on my last comment.
|
@m-abs I was also able to workaround this using :
But it definitely seems like overhead. |
Let's do a little summary so far Subscriptions And CleanUpWhen using page-navigation However, you might want prevent it from updating while not shown. So far I think @m-abs solution of using page events would be the simplest. You might call the Using ActivatedRoute and ParamsNormally, in Angular you would inject In NativeScript when navigating back to an existing page your component will not be re-created. Angular router will still create an new instance The Solution: In NativeScript you can inject So, instead of:
You do:
Why So ComplicatedNative platforms preserve the views in the native navigation stack when navigating deeper. So we need to preserve the components that are connected to those views too. This is done in the |
Thanks :D So great, use loadedEvent, unloadedEvenet to call ngOnInit and ngOnDestroy solved all problems |
Awesome! Page.unloadedEvent on ngOnInit solved my problem. @m-abs Thanks alot. |
It still doesn't work for me. Any idea where I'm doing wrong?
@bunnyvishal6 @m-abs able to help? |
@MrNicodemuz Sent from my OnePlus ONEPLUS A3003 using FastHub |
@m-abs many thanks it's working now! :) |
@m-abs @gergderkson Guys, my app is closed after call |
Hi, I solved the issue, triggering change detection in my root component, every time any navigation ends. this.router.events.subscribe(e => e instanceof NavigationEnd && this.changeDetectionRef.detectChanges()); |
This is one of the most annoying "features" of NS Angular and it doesn't seem like anyone's looking to improve it. We've spent so much time debugging issues where resources haven't been released and streams are just piling up because ngOnDestroy is not getting called. The biggest problem here is inconsistency - the constructor and ngOnInit is getting called every time the component is navigated to. But when navigating out, you're only left with the router event and page unloaded event. Getting rid of ng events and just using Page events would seem like a good solution but the problem is - the page is unloaded every time app goes to background. Some of the app logic needs to keep running in the background, so this is not a good solution either. |
Closing the issue - for anyone interested in how the native mobile events are working and should be used alongside the Angular ones please read this comment |
I wrote an article about this. I solved it in generic way with the decorator. |
Man, so much for code re-use. Now I have to create an entirely separate tns controller just to use the |
This remains a major limitation / "feature" :/ |
Why if Im writing a wizard, i need to move forward and backward, and sometimes move, skip, or jump to pages/views and limit user capabilities of moving back to pages (i could set a new root here, but not my case) not catched or in any state. Also if i complete the wizard i should be able to ngDestroy all the previous views/pages to do a clean new wizard setup. Current logic is not allowing me to do that, and i do not wanna recreate the wheel or change the native behavior, is there a way to trigger those events or angular cycle hooks (init, destroy, etc) using nativescript? |
I ran into this problem and I'll try to explain it here.
With
nativescript-angular
the lifecycle hookngOnDestroy
isn't called when we navigate away from a page as it would be on web. Navigating back reuses the old component withnativescript-angular
but a new instance is created on web.I've based an example on Nathan Walker's
angular2-seed-advanced
:https://github.com/m-abs/angular2-seed-advanced/tree/ngondestroy-not-triggered
I've two component for my routes:
HomeComponent
AboutComponent
Each component have a
instanceNo
, so we can identify multiple instances of the component.As you can see the two behaves very differently.
On web:
With nativescript-angular:
If you want to try for yourself, checkout my branch
ngondestroy-not-triggered
and run:For web:
For nativescript-angular with android:
Shouldn't the two behave the same?
So components are destroyed when we navigate from a page and (re)created when we navigate to the page?
The text was updated successfully, but these errors were encountered: