路由器导航在导航后不保存URL

发布于 2025-02-10 17:24:35 字数 864 浏览 1 评论 0原文

应用程序路由:

  {
        path: '',
        component: IndexComponent,
        children: [
            {path: 'table', component: TimetablesStaticComponent},
            {
                path: 'comparison/:id',
                component: TimetablesAllComponent,
                children: [
                    {
                        path: 'table',
                        component: TimetablesStaticComponent,
                    },
                ],
            },
        ],
    },

我可以从默认根中导航到表:localhost:4200/table,它加载了timetablesstaticcomponent。但是在服务中,我想导航到表组件并保存以前的路由。我有路由localhost:4200/comparison/123。从服务中我调用方法this.router.navigate(['table'],{relativeto:this.activatedRoute}); 它导航到localhost:4200/table,但应为localhost:4200/comparion/123/table 我在做什么错?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧城空念 2025-02-17 17:24:35

ActivatedRoute对于每个路由组件都不同。

注入timetablesAllComponent它相对于比较/3,在timetablestaticcomponent中,它是beambormiss/123/table

如果您在服务中使用它是根路由(“”)。您需要从组件中调用导航 - 在timetablesallcomponent中。不是来自服务。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文