angular - Ionic 4: Redirect to route with id param
问题描述
Let's say in our hero angular 7/ionic 4 hero app we have a HeroDetailsRoutingModule
with 2 Tabs, the SuperpowersPage
and the CostumePage
.
Now we want to define the SuperpowersPage
as default tab.
So I tried to set up my HeroDetailsRoutingModule:
const routes: Routes = [
{
path: 'tabs/',
component: HeroDetailsPage,
children: [
{
path: 'superpowers',
loadChildren: './superpowers/superpowers.module#SuperpowersPageModule'
},
{
path: 'costume',
loadChildren: './costume/costume.module#CostumePage'
},
{
path: '',
redirectTo: '/heroes/:heroId/tabs/superpowers',
pathMatch: 'full'
}
]
},
{
path: '',
redirectTo: '/heroes/:heroId/tabs',
pathMatch: 'full'
}
];
Of course that does not work because we don't have the :heroId
in this module. What is the correct way to redirect in this case? I tried to do a relative path instead (e.g. redirectTo: './tabs/superpowers'
) but that did not work either. I have the feeling to commit some obvious mistake but I don't get it. Thanks in advance!
解决方案
我以这种方式解决了问题
在 HeroDetailsRoutingModule 中将 Routes 定义为
const routes: Routes = [
{
path: 'tabs',
component: HeroDetailsPage,
children: [
{
path: 'superpowers',
children:[
{ path : ':heroId',
loadChildren: './superpowers/superpowers.module#SuperpowersPageModule'
}
]
},
{
path: 'costume',
loadChildren: './costume/costume.module#CostumePage'
}
]
},
{
path: '',
redirectTo: 'tabs/superpowers',
pathMatch: 'full'
}
];
在 hero-details.page.ts 中设置 heroId 的值
heroId = <value>// get the value of heroId
然后在 hero-details.page.html 文件访问选项卡中为
<ion-tab-button tab="superpowers/{{heroId}}">
推荐阅读
- python - PyInstaller 从 .py 转换为 .exe 时无法执行脚本
- reactjs - 在父组件的 DOM 层次结构之外插入组件的新实例 onClick
- html - 隐藏特定组件中的导航栏
- ruby-on-rails - graphql ruby 单元测试
- leaflet - 如何在javascript中刷新传单地图
- r - 在使用 r 中的 auto.arima 进行预测之前,我应该使用 ts 函数吗?
- amazon-web-services - 来自 AWS 中私有子网的出站流量
- azure - 如何恢复到 Azure Function App 的经典身份验证
- ios - prefersLargeTitles 属性中文本的字体属性是什么?
- python - 调用 tf.data.Dataset.map 时未更新全局变量