Cookies concent notice

This site uses cookies from Google to deliver its services and to analyze traffic.
Learn more
Skip to main content
This site is no longer updated.Head to Angular.devHome
/

This is the archived documentation for Angular v17. Please visit angular.dev to see this page for the current version of Angular.

RouterState

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

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

See also

Properties

Property Description
snapshot: RouterStateSnapshot

The current snapshot of the router state

Methods

      
      toString(): string
    
Parameters

There are no parameters.

Returns

string

Usage notes

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);
    //...
  }
}