gpt4 book ai didi

angular - 如何在 Angular 中从同级组件导航到另一个同级组件?

转载 作者:行者123 更新时间:2023-12-04 00:05:05 25 4
gpt4 key购买 nike

下面是我的app.routing.ts

export const Approute: Routes = [
{
path: 'login',
component: LoginComponent
},
{
path: 'home',
component: HomeComponent,
children: [
{
path: 'dashboard',
component: DashboardComponent
},
{
path: 'trip-details',
component: TripDetailsComponent
}
]
},
{ path: 'not-found', component : NotFoundComponent },
{ path: '**', redirectTo: '/not-found' }
];

component.ts

 viewDetails(details) {
...
this.route.navigate(['../trip-details']);
}

上面我有一个组件并试图导航到另一个同级组件但无法导航到 http://localhost:4200/home/trip-details 它将我重定向到 http://localhost:4200/not-found.

哪里做错了?

最佳答案

你可以指定relativeTo如下图所示:

import {Router, ActivatedRoute } from '@angular/router';
constructor(private r: ActivatedRoute) { }

viewDetails(details) {
...
this.route.navigate(['../trip-details'], { relativeTo: this.r });
}

关于angular - 如何在 Angular 中从同级组件导航到另一个同级组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49487197/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com