angular - Angular 路由器:带参数的 loadChildren
问题描述
在我的角度路由器中,我想根据 url 中的参数延迟加载不同的模块。这是我的设置的psudo路线:
path: ':slug',
loadChildren: async () => {
const SLUG = "How do I get the :slug param from the URL"?;
switch (SLUG) {
case 'first':
return (await import('./pages/first-page/first-page.module')).FirstPageModule;
break;
case 'second':
return (await import('./pages/second-page/second-page.module')).SecondPageModule;
break;
default:
return (await import('./pages/default-page/default-page.module')).DefaultPageModule;
}
},
我尝试使用解析器和守卫——不幸的是,它们在路由激活后触发,因此我无法事先访问参数。有任何想法吗?
解决方案
slug
为like
创建一个模块:const routes: Routes = [ { path: 'slug', loadChildren: () => import('./slug/slug.module').then(m => m.SlugModule) } ];
然后,在 中SlugModule
,为组件添加路由,例如:
const routes: Routes = [ { path: 'first', component: FirstComponent }, { path: 'second', component: SecondComponent } ];
推荐阅读
- python - 链式操作中重命名DataFrame的索引
- verilog - ROM 4kx8 verilog,使用 $readmemb 读取文件时出现问题
- laravel - laravel 请求周期会在每次页面刷新时重复吗?
- javascript - Javascript 使用键映射和正则表达式替换
- java - JDA Discord - 如何在数组中获取对机器人消息做出反应的用户的所有名称
- python - 如何将函数返回值存储在其他函数中
- kotlin - 我很好奇 Kotlin 的 toList 和 IntArray 的速度差异
- php - 如何使用 POST 方法构造 API URL
- node.js - 在 Jenkins 中从 Node / package.json 获取项目版本
- java - 下界斯卡拉