app routing:
{
path: '',
component: IndexComponent,
children: [
{path: 'table', component: TimetablesStaticComponent},
{
path: 'comparison/:id',
component: TimetablesAllComponent,
children: [
{
path: 'table',
component: TimetablesStaticComponent,
},
],
},
],
},
I can navigate to table from default root: localhost:4200/table
, and it loads TimetablesStaticComponent. But in service, i'd like to navigate to table component and save previous route. I have route localhost:4200/comparison/123
. And from service i call method this.router.navigate(['table'], {relativeTo: this.activatedRoute});
And it navigates to localhost:4200/table
, but should be localhost:4200/comparison/123/table
What am i doing wrong?
CodePudding user response:
ActivatedRoute
is different for each routed component.
Injected into TimetablesAllComponent
it is relative to comparison/3
and in TimetablesStaticComponent
it is comparison/123/table
If you use it in service it is the root route (""
). You need to call the navigation from the component - in your case in TimeTablesAllComponent
. Not from the service.