将动态参数添加到angular2 +路径中

问题描述 投票:0回答:1

我的角度应用程序中有以下路线:

{ path: '', redirectTo: '/exercise-log', pathMatch: 'full'},
{ path: 'exercise-log/:date', component: ExerciseListComponent },
{ path: 'exercise-log', component: ExerciseListComponent }

我想删除exercise-log并将''重定向到'exercise-log/:date',然后改用今天的日期。

我该怎么做?我曾考虑过要在route模块的构造函数中设置一个date变量,但这感觉很不对。更新:尝试在构造函数中设置它似乎不是现在尝试的方法。

angular url-routing angular-routing angular-router
1个回答
0
投票

您可以在不带参数的情况下向路由添加CanActivate路由保护,并且在重新路由至canActivate路由时始终在exercise-log/currentDate方法中返回false。

然而,这似乎有点不可靠,所以可能有更好的方法来实现这一目标。


0
投票

您只需将其与当前时间重定向到exercise-log/:date{path:'exercise-log',redirectTo:'/ exercise-log /'+ Date.now(),pathMatch:'full'}

© www.soinside.com 2019 - 2024. All rights reserved.