首页 > 解决方案 > Angular - 获取当前激活的路线并使用不同的路线参数重新导航到同一页面

问题描述

在更改标题中搜索文本框中的 itemCode 后,我试图获取当前路线并导航到同一路线。当我最初来到页面时,父路由是“iauth/iauthedit/1706”,然后用户导航到子路由“/info”,还有其他几条用户可以从侧面导航栏导航的路线,如下图所示. 如图所示,当我将应用程序标头中的项目代码更改为 1853 时,URL 应更改为“iauth/iauthedit/1853/info”,如何根据当前路由 URL 动态更改浏览器 URL 路由参数浏览器?,我正在尝试从浏览器 URL 中找到当前场景父级“iauth/iauthedit/”中的当前激活路由,以便进行导航

this.router.navigate(['iauth/iauthedit/', 1853];

图片

以下是目前的路线:

const itemAuthRoutes: Routes = [
    {
        path: 'iauthedit/:itemCode',
        children: [
            {
                path: '',
                canActivate: [AuthGuard],
                //data: { feature: AppFeature.AuthorizedItem },
                component: ItemAuthorizationEditComponent,
                children: [
                    {
                        path: 'info', component: ItemAuthorizationInfoEditComponent
                    }]
            }]
    }]

标签: angulartypescriptangular-routingangular-routerlink

解决方案


您可以尝试遍历routeConfig'spath属性

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

navigate(newId: string) {
  let route = this.activatedRoute.snapshot;
  const parts = [];
  while (route) {
    route.routeConfig && route.routeConfig.path && parts.push(route.routeConfig.path);
    route = route.parent;
  }
  // `parts` now contain your route's pieces - do whatever you deem fit with that
  parts[1] = newId; // i.e. replace the part that needs replacing
  this.router.navigate(parts.reverse());
}

希望这有所帮助 :-)


推荐阅读