最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Ionic Angular lifecycle hooks are not triggered when a page is "pushed" - Stack Overflow

programmeradmin4浏览0评论

I have nested routes in my Ionic-Angular app. When I navigate from one child route to another child route within the same parent route, the Ionic lifecycle hooks work as expected. But when navigating to a different parent route, no lifecycle hook is fired - neither from Ionic nor from Angular.

Here is my routes setup:

import { NgModule } from '@angular/core';
import { PreloadAllModules, RouterModule, Routes } from '@angular/router';

const routes: Routes = [
  { path: '', component: HomeComponent },
  { path: ':entityId', component: HomeComponent },
  {
    path: 'account/entity',
    component: DashboardPage,
    data: { tab: Tab.Business },
    canActivate: [authGuard],
    children: [
      { path: '', redirectTo: 'pages', pathMatch: 'full' },
      { path: 'pages', component: PagesComponent, canActivate: [entityGuard] },
      { path: 'pages/:entityId', component: EntityReadComponent, canActivate: [entityGuard], canDeactivate: [cleanupGuard] },
      { path: 'subscription', component: SubscriptionComponent, canActivate: [entityGuard] },
    ]
  },
  { path: '**', component: NotFoundComponent, data: { tab: Tab.None } }
];
@NgModule({
  imports: [
    RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })
  ],
  exports: [RouterModule]
})
export class AppRoutingModule { }

The DashboardPage component is a custom tabs component that simply routes to different pages by using routerLink.

Inside my EntityReadComponent file I have this:

export class EntityReadComponent implements ViewDidEnter {
  ionViewDidEnter() {
    console.log('vied entered...');
  }
}

The ionViewDidEnter lifecycle hook will fire when navigating to a sibling route, let's say, to subscription, but will not fire when navigating to /:entityId.

I use Angular version 18 and Ionic version 8.4.1.

I have nested routes in my Ionic-Angular app. When I navigate from one child route to another child route within the same parent route, the Ionic lifecycle hooks work as expected. But when navigating to a different parent route, no lifecycle hook is fired - neither from Ionic nor from Angular.

Here is my routes setup:

import { NgModule } from '@angular/core';
import { PreloadAllModules, RouterModule, Routes } from '@angular/router';

const routes: Routes = [
  { path: '', component: HomeComponent },
  { path: ':entityId', component: HomeComponent },
  {
    path: 'account/entity',
    component: DashboardPage,
    data: { tab: Tab.Business },
    canActivate: [authGuard],
    children: [
      { path: '', redirectTo: 'pages', pathMatch: 'full' },
      { path: 'pages', component: PagesComponent, canActivate: [entityGuard] },
      { path: 'pages/:entityId', component: EntityReadComponent, canActivate: [entityGuard], canDeactivate: [cleanupGuard] },
      { path: 'subscription', component: SubscriptionComponent, canActivate: [entityGuard] },
    ]
  },
  { path: '**', component: NotFoundComponent, data: { tab: Tab.None } }
];
@NgModule({
  imports: [
    RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })
  ],
  exports: [RouterModule]
})
export class AppRoutingModule { }

The DashboardPage component is a custom tabs component that simply routes to different pages by using routerLink.

Inside my EntityReadComponent file I have this:

export class EntityReadComponent implements ViewDidEnter {
  ionViewDidEnter() {
    console.log('vied entered...');
  }
}

The ionViewDidEnter lifecycle hook will fire when navigating to a sibling route, let's say, to subscription, but will not fire when navigating to /:entityId.

I use Angular version 18 and Ionic version 8.4.1.

Share Improve this question asked yesterday SchnitzlerSchnitzler 3182 silver badges14 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

To effectively manage component lifecycle events when navigating between different parent routes in your app, you can try subscribing to Angular Router events. Specifically, listen for the NavigationEnd event to trigger actions once navigation is complete.

Additionally, implement the OnInit and OnDestroy lifecycle hooks to perform tasks when your component initializes and is destroyed. This combination will help you manage state and execute necessary logic seamlessly across route changes. Ionic lifecycle hooks are not that good at triggering.

发布评论

评论列表(0)

  1. 暂无评论