Angular 7嵌套模块路由

时间:2019-03-13 13:32:34

标签: angular ionic-framework routing ionic4

我不知道如何使用多个嵌套模块进行路由。 RouteTree对我来说看起来不错且正确 enter image description here

它可以工作到“更多”页面。单击ContactPage后,URL会更改,但视图不会呈现。

这是我的路由器的样子:

app.routing.module

const routes: Routes = [
  { path: '', loadChildren: './pages/tabs/tabs.module#TabsPageModule' }
];

@NgModule({
  imports: [
      RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })
  ],
  exports: [RouterModule]
})
export class AppRoutingModule {}

tabs.routing.module

const routes: Routes = [
    {
        path: 'tabs',
        component: TabsPage,
        children: [
            {
                path: 'more',
                loadChildren: '../more/more.module#MorePageModule'
            },
            {
                path: '',
                redirectTo: 'home',
                pathMatch: 'full'
            }
        ]
    },
    {
        path: '',
        redirectTo: 'tabs/home',
        pathMatch: 'full'
    }
];

@NgModule({
    imports: [
        RouterModule.forChild(routes)
    ],
    exports: [RouterModule]
})
export class TabsPageRoutingModule {
}

more.routing.module

const routes: Routes = [
    {
        path: '',
        component: MorePage,
        children: [
            {
                path: 'contact',
                component: ContactPage
            },
        ]
    }
];

@NgModule({
    imports: [
        RouterModule.forChild(routes)
    ],
    exports: [RouterModule]
})
export class MoreRouterModule {
}

contact.module

const routes: Routes = [
  {
    path: 'contact',
    component: ContactPage
  }
];

@NgModule({
  imports: [
    SharedModule,
    RouterModule.forChild(routes)
  ],
  declarations: [ContactPage],
  providers: [
  ]
})
export class ContactPageModule {}

我不知道问题是什么。有什么我想念的吗?

1 个答案:

答案 0 :(得分:1)

请尝试重写以下内容(在more.routing.module中):

const routes: Routes = [
    {
        path: '',
        component: MorePage,
        children: [
            {
                path: 'contact',
                component: ContactPage
            },
        ]
    }
];

像这样使用loadChildren

children: [
    {
        path: 'contact',
        loadChildren: 'path/to/contact.module#ContactPageModule'
    },
]