relativeTo ?: ActivatedRoute | null
允许从当前激活的路由进行相对导航。
Specifies a root URI to use for relative navigation.
比如,考虑下列路由器配置,parent 路由拥有两个子路由。
For example, consider the following route configuration where the parent route has two children.
[{ path: 'parent', component: ParentComponent, children: [{ path: 'list', component: ListComponent },{ path: 'child', component: ChildComponent }] }]
content_copy
[{
path : 'parent' ,
component : ParentComponent ,
children : [{
path : 'list' ,
component : ListComponent
},{
path : 'child' ,
component : ChildComponent
}]
}]
下面的 go()
函数会把目标 URI 解释为相对于已激活路由 child
的,从而导航到 list
路由。
The following go()
function navigates to the list
route by interpreting the destination URI as relative to the activated child
route
@
Component ({...}) class ChildComponent { constructor(private router:
Router , private route:
ActivatedRoute ) {} go() { this.router.navigate(['../list'], { relativeTo: this.route }); } }
content_copy
@ Component ({...})
class ChildComponent {
constructor ( private router : Router , private route : ActivatedRoute ) {}
go () {
this . router . navigate ([ '../list' ], { relativeTo : this . route });
}
}
queryParams ?: Params | null
设置 URL 的查询参数。
Sets query parameters to the URL.
// Navigate to /results?page=1 this.router.navigate(['/results'], { queryParams: { page: 1 } });
content_copy
// Navigate to /results?page=1
this . router . navigate ([ '/results' ], { queryParams : { page : 1 } });
fragment ?: string
设置 URL 的哈希片段(#
)。
Sets the hash fragment for the URL.
// Navigate to /results#top this.router.navigate(['/results'], { fragment: 'top' });
content_copy
// Navigate to /results#top
this . router . navigate ([ '/results' ], { fragment : 'top' });
queryParamsHandling ?: QueryParamsHandling | null
如何在路由器链接中处理查询参数以进行下一个导航。为下列值之一:
How to handle query parameters in the router link for the next navigation. One of:
“preserve” 选项将放弃所有新的查询参数:
The "preserve" option discards any new query params:
// from /view1?page=1 to/view2?page=1 this.router.navigate(['/view2'], { queryParams: { page: 2 }, queryParamsHandling: "preserve" });
content_copy
// from /view1?page=1 to/view2?page=1
this . router . navigate ([ '/view2' ], { queryParams : { page : 2 }, queryParamsHandling : "preserve"
});
“merge” 选项会将新的查询参数附加到当前 URL 的参数中:
The "merge" option appends new query params to the params from the current URL:
// from /view1?page=1 to/view2?page=1&otherKey=2 this.router.navigate(['/view2'], { queryParams: { otherKey: 2 }, queryParamsHandling: "merge" });
content_copy
// from /view1?page=1 to/view2?page=1&otherKey=2
this . router . navigate ([ '/view2' ], { queryParams : { otherKey : 2 }, queryParamsHandling : "merge"
});
queryParams
对象中的参数之间发生键名冲突,则使用新值。
In case of a key collision between current parameters and those in the queryParams
object, the new value is used.
preserveFragment ?: boolean
在后续导航时保留 #
片段
When true, preserves the URL fragment for the next navigation
// Preserve fragment from /results#top to /view#top this.router.navigate(['/view'], { preserveFragment: true });
content_copy
// Preserve fragment from /results#top to /view#top
this . router . navigate ([ '/view' ], { preserveFragment : true });