Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Router navigate doesn't save url after navigate

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading