代码之家  ›  专栏  ›  技术社区  ›  Justin

如何向ActivatedRoute添加段?

  •  2
  • Justin  · 技术社区  · 7 年前

    在angularJS中,当使用UI路由器时,基本上可以命名路由,因为每个州都有一个名称,并且可以导航到该路由。

    例如: $stateProvider.state("base", { url: "/", controller: "baseController" }); 然后,要导航到该路线,您可以执行以下操作: $state.go("base");

    我也在尝试做同样的事情。2与 激活路由 .

    我所在的组件有一条路线: base/:acctId/:session/upsells/:id/:type

    我想导航到 base/:acctId/:session/config/:id/:type

    我想在ActivatedRoute中添加一个动态段来填充 config/:id 这样,当我使用路由器导航时,我可以附加 :type .

    从我所处的组件来看( base/:acctId/:session/upsells/:id/:type ),我想在ActivatedRoute中添加几段并调用后导航到另一条路线: this.router.navigate([type], { relativeTo: this.activatedRoute }

    文档中没有太多关于修改ActivatedRoute的内容,我也尝试过修改参数,但路由器似乎不使用参数来导航。我试过做类似的事情 Viktor Savkin's comment on this github issue 但我还是不确定路由器。导航函数的行为。

    我用过 this.router.createUrlTree(['./config', configId, idd], { relativeTo: this.route.parent}) 要生成一个url树,这正是我所需要的,但问题是,这个。路由器。navigate函数采用ActivateRoute对象来相对导航,而不是UrlTree对象。是否有从url树创建ActivateRoute对象的映射?

    我的路由器配置如下所示:

    const routes: Routes = [
        {
            path: "base/:acctId/:session",
            children: [
                {
                    path: "config/:configid/:idd",
                    component: ListViewItemDetailComponent,
                    children: [
                        {
                            path: "type1", <----- i want to get here
                            component: type1Component
                        },
                        {
                            path: "type2",
                            component: type2Component
                        },
                        {
                            path: "type3",
                            component: type3Component
                        }
                    ]
                },
                {
                    path: "upsells/:id/:type", <------ I am here
                    component: UpsellsComponent
                }
            ]
        }
    ]
    

    如果这些都不合理,请告诉我,谢谢你抽出时间。

    1 回复  |  直到 7 年前
        1
  •  1
  •   planet_hunter    7 年前

    看看这个 stackblitz POC 我添加了相同的路由配置。

    基本上,我使用了以下路由逻辑来路由到指定的 type1 路线-

    this._router.navigate(['./config/1/2/type1'], {
      relativeTo: this._ActivatedRoute.parent
    });
    

    this._ActivatedRoute.parent 到达父路由即。 base/:acctId/:session &然后给出了到达的相对路径 类型1 维兹。 ./config/1/2/type1

    我希望这有帮助。