angular - 带有 loadchildren 的 Angular 延迟加载路由
问题描述
我有一个具有标签和登录页面的离子应用程序。选项卡组件是它自己的模块,每个选项卡都有一个路由模块,每个选项卡都是它自己的模块。当应用程序加载时,我想定向到登录页面,然后在登录时重定向到选项卡组件之一的选项卡选项卡(菜单/主页)。
const routes: Routes = [
{
path: '',
loadChildren: () => import('./tabs/tabs.module').then(m =>
m.TabsPageModule)
},
{
path: 'login',
component: LoginComponent
},
{
path: "",
component: LoginComponent
},
{
path: '',
redirectTo: '',
pathMatch: 'full'
}
];
@NgModule({
imports: [
RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })
],
exports: [RouterModule]
})
export class AppRoutingModule {}
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { TabsPage } from './tabs.page';
const routes: Routes = [
{
path: 'menu',
component: TabsPage,
children: [
{
path: 'home',
loadChildren: () => import('./home/home.module').then(m => m.HomePageModule)
},
{
path: 'library',
loadChildren: () => import('./library/library.module').then(m => m.LibraryPageModule)
},
{
path: 'search',
loadChildren: () => import('./search/search.module').then(m => m.SearchPageModule)
},
{
path: 'profile',
loadChildren: () => import('./profile/profile.module').then(m => m.ProfileModule)
},
{
path: '',
redirectTo: 'menu/home',
pathMatch: 'full'
}
]
},
{
path: '',
redirectTo: '/menu/home',
pathMatch: 'full'
}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class TabsPageRoutingModule {}
解决方案
将您的标签路由更改为:-
const routes: Routes = [
{
path: 'menu',
children: [
{
path: '',
component: TabsPage
},
{
path: 'home',
loadChildren: () => import('./home/home.module').then(m => m.HomePageModule)
},
{
path: 'library',
loadChildren: () => import('./library/library.module').then(m => m.LibraryPageModule)
},
{
path: 'search',
loadChildren: () => import('./search/search.module').then(m => m.SearchPageModule)
},
{
path: 'profile',
loadChildren: () => import('./profile/profile.module').then(m => m.ProfileModule)
},
{
path: '',
redirectTo: 'menu/home',
pathMatch: 'full'
}
]
},
{
path: '',
redirectTo: '/menu/home',
pathMatch: 'full'
}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class TabsPageRoutingModule {}
或者
在 TabsPage 中放置一个路由器插座
要了解原因,请阅读:- https://link.medium.com/U5u4d4ub66
推荐阅读
- java - 简化java中复杂的if语句
- java - 无法在 Redis 数据存储中存储数据 - Spring Boot
- git - Git忽略文件在Windows 10下不起作用
- python - IndexError:列表索引超出范围 - Python/Arduino
- python - 根据条件获取索引和列
- laravel - 如何在 Laravel Passport 中创建角色?
- docker - 在 Flask 中使用 Sentry 查找 Heroku H10 错误的原因
- angular - 配置中的 Angular 设置构建命令行参数(--host / --disable-host-check for --configuration=production)
- laravel - Dockerize Laravel 流明
- django - 从 POST 获取 form.cleaned_data 后如何将其传递给另一个视图?