Angular 如何将段添加到ActivatedRoute?
在angularJS中,当使用UI路由器时,基本上可以命名路由,因为每个州都有一个名称,并且可以导航到该路由 例如:Angular 如何将段添加到ActivatedRoute?,angular,angular-router,angular-activatedroute,Angular,Angular Router,Angular Activatedroute,在angularJS中,当使用UI路由器时,基本上可以命名路由,因为每个州都有一个名称,并且可以导航到该路由 例如: $stateProvider.state(“base”,{url:/”,controller:“baseController”}) 然后,要导航到该路线,您可以执行以下操作: $state.go(“base”) 我正试图在angular v5.2中使用ActivatedRoute做同样的事情 我所在的组件的路由为:base/:acctId/:session/upsells/:id
$stateProvider.state(“base”,{url:/”,controller:“baseController”})代码>
然后,要导航到该路线,您可以执行以下操作:
$state.go(“base”)代码>
我正试图在angular 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}
文档中没有太多关于修改ActivatedRoute的内容,我尝试过修改参数,但路由器似乎没有使用这些参数进行导航。我尝试过类似的操作,但我仍然不确定Router.navigate函数的行为
我使用了this.router.createUrlTree(['./config',configId,idd],{relativeTo:this.route.parent})
生成的url树正是我所需要的,但问题是this.router.navigate函数使用ActivatedRoute对象来相对导航,而不是url树对象。是否有从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
}
]
}
]
const routes:routes=[
{
路径:“base/:acctId/:session”,
儿童:[
{
路径:“config/:configid/:idd”,
组件:ListViewItemDetailComponent,
儿童:[
{
路径:“type1”,在我添加了相同路由配置的地方检查此项
基本上,我使用了以下路由逻辑来路由到指定的type1
路由-
this._router.navigate(['./config/1/2/type1'], {
relativeTo: this._ActivatedRoute.parent
});
this.\u ActivatedRoute.parent
到达父路由,即base/:acctId/:session
&然后给出到达type1
的相对路径,即/config/1/2/type1
我希望这能有所帮助。为了更好地理解路由段是如何配置的,您还必须添加路由配置。路由器中的relativeTo
选项。导航
将取决于路由配置。@planet\u hunter哦,对不起。我添加了路由配置。嗯,这不是真正的问题我正在寻找。我会更新我的问题以尝试澄清。谢谢你花时间回复。