Search code examples
angularurl-routingangular-routingangular-router

Add a dynamic parameter to an angular2+ route


I have the following routes in my angular app:

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

I want to remove exercise-log and redirect '' to the 'exercise-log/:date' one and put today's date in instead.

How can I do this? I had thought about having a date variable that gets set in the constructor of the route module but that feels wrong. UPDATE: trying to set it in a constructor doesn't seem to be the way having now tried it.


Solution

  • You can simply redirect it to exercise-log/:date with current time

    { path: 'exercise-log', redirectTo: '/exercise-log/' + Date.now(), pathMatch: 'full' }