Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix component not loading in Angular Ionic

I'm trying to lazy load a component in ionic Angular and I've gotten the route (../app/buildings/:buildingId) to not throw errors when i navigate to it but it doesn't load the proper component in.

I think it's defaulting to the parent, but i don't know how to fix it.. I've tried some things (not lazy loading / putting it all in the app.module) but i can't seem to fix it. Any help is appreciated.

tabs.router.module.ts

 {
    path: 'app',
    component: TabsPage,
    children: [
      {
        path: 'home',
        children: [
          {
            path: '',
            loadChildren: '../tab1/tab1.module#Tab1PageModule'
          }
        ]
      },
      {
        path: 'buildings',
        children: [
          {
            path: '',
            component: Tab2Page,
            resolve: {buildings: BuildingResolverService},
            loadChildren: '../tab2/tab2.module#Tab2PageModule'
          }
        ]
      },
      {
        path: 'tab3',
        ...

tab2.module.ts

export const tab2Routes: Routes = [
  {
    path: ':buildingId',
    component: BuildingComponent
  }
];
@NgModule({
  exports: [RouterModule],
  imports: [
    IonicModule,
    CommonModule,
    FormsModule,
    RouterModule.forChild(tab2Routes)
  ],
  declarations: [
    BuildingComponent
  ]
})
export class Tab2PageModule {}


Going to /buildings loads the proper component, /buildings/1 does not. I would expect the second route to do the same, I think it's defaulting to the first one because that one matches too maybe? i'm not sure how to fix it if that is the case.

I hope I've been clear enough

like image 876
Souf Avatar asked Jul 18 '26 02:07

Souf


2 Answers

Your tab2Routes should have route '' as your default Tab2Component and :buildingId as the BuildingComponent (like you already have):

export const tab2Routes: Routes = [
  { 
    path: '', 
    component: Tab2Page 
  },
  {
    path: ':buildingId',
    component: BuildingComponent
  }
];
like image 82
AT82 Avatar answered Jul 21 '26 15:07

AT82


Lazyload in Ionic is quiet different from a standard Angular project. Lazy loading is automatically setted up by the ionic page system. You can find more informations here.

like image 22
Franck Abgrall Avatar answered Jul 21 '26 16:07

Franck Abgrall



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!