RouterState
将路由器的状态表示为已激活路由的树。
Represents the state of the router as a tree of activated routes.
class RouterState extends Tree {
snapshot: RouterStateSnapshot
toString(): string
}
参见
构造函数
参数
|
属性
| 属性 | 说明 |
|---|---|
snapshot: RouterStateSnapshot | 声明在构造函数中 The current snapshot of the router state |
方法
参数没有参数。 返回值
|
使用说明
路由树中的每个节点都是 ActivatedRoute 的实例,该实例了解“已消耗的” URL 段,已提取的参数和已解析的数据。使用 ActivatedRoute 属性可以从任何节点遍历树。
Every node in the route tree is an ActivatedRoute instance that knows about the "consumed" URL segments, the extracted parameters, and the resolved data. Use the ActivatedRoute properties to traverse the tree from any node.
以下片段显示了组件如何获取当前状态的根节点以建立其自己的路由树:
The following fragment shows how a component gets the root node of the current state to establish its own route tree:
@Component({templateUrl:'template.html'})
class MyComponent {
constructor(router: Router) {
const state: RouterState = router.routerState;
const root: ActivatedRoute = state.root;
const child = root.firstChild;
const id: Observable<string> = child.params.map(p => p.id);
//...
}
}