填寫這份《一分鐘調查》,幫我們(開發組)做得更好!去填寫Home

RouterState

將路由器的狀態表示為已啟用路由的樹。

Represents the state of the router as a tree of activated routes.

      
      class RouterState extends Tree {
  snapshot: RouterStateSnapshot
  toString(): string
}
    

參見

建構函式

      
      constructor(root: TreeNode<ActivatedRoute>, snapshot: RouterStateSnapshot)
    
引數
root TreeNode
snapshot RouterStateSnapshot

The current snapshot of the router state

屬性

屬性說明
snapshot: RouterStateSnapshot宣告在建構函式中

The current snapshot of the router state

方法

      
      toString(): string
    
引數

沒有引數。

返回值

string

使用說明

路由樹中的每個節點都是 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);
    //...
  }
}