Angular 6我怎样才能回到父母然后来到router.navigate()的孩子

问题描述 投票:1回答:1

嗨,我有父组件'Order'和两个子组件'History'和'Orders'。当前网址是order/history/id我想回到'订单'组件like order/orders/id所以我在我的应用程序中使用路由器。

this.router.navigate(['../../orders', orderId]); 

How do I navigate to a parent route from a child route?中建议的历史组件,但它显示cannot match any routes url segment orders/7y6786解决这个问题的最佳方法是什么?谢谢。

angular7 angular-router
1个回答
1
投票

当您在组件内部使用导航时,您可以使用ActivatedRoute服务。

所以在历史组件中,尝试这样做:

constructor(
  private route: ActivatedRoute,
  private router: Router
) {}

navigateToOrders() {
  this.router.navigate(['orders', orderId], { relativeTo: this.route.parent })
}
© www.soinside.com 2019 - 2024. All rights reserved.