在angularJS中,当使用UI-Router时,您可以实际命名路由,因为每个州都有一个名称,您可以导航到该路由 .
例如: $stateProvider.state("base", { url: "/", controller: "baseController" });
然后导航到该路线您可以执行以下操作: $state.go("base");
我试图在角度v5.2中用 ActivatedRoute 做同样的事情 .
我所在的组件有以下路线: 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 }
没有't much in the documentation about modifying the ActivatedRoute and I have tried modifying the params but it seems that the Router doesn'吨使用参数来导航 . 我尝试过类似于Viktor Savkin's comment on this github issue的东西,但我又不确定Router.navigate函数的行为 .
我已经使用了 this.router.createUrlTree(['./config', configId, idd], { relativeTo: this.route.parent})
来生成一个正是我需要的url树,但问题是this.router.navigate函数使用ActivatedRoute对象来相对而不是UrlTree对象进行导航 . 是否有 Map 从url树创建ActivatedRoute对象?
我的路由器配置如下所示:
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 回答
检查此stackblitz POC我添加了相同的路由配置 .
基本上,我使用以下路由逻辑路由到指定的
type1
路由 -this._ActivatedRoute.parent
到达父路线即 .base/:acctId/:session
然后给出相对路径以达到type1
即 ../config/1/2/type1
我希望这有帮助 .