liveBook · Manning

ActivatedRouteSnapshot

As explained in Pro Angular 16

ActivatedRouteSnapshot is an Angular class representing the state of an activated route at a particular point during navigation, including captured parameters.

ActivatedRouteSnapshot captures information about an activated route. In a component, inject ActivatedRoute and read its snapshot to inspect the URL that led to the component being displayed. This lets the URL determine application behavior instead of relying on events from a shared state service. section 24.3.1

Table 24.6. The Basic ActivatedRouteSnapshot properties
Name
Description
url
This property returns an array of UrlSegment objects, each of which describes a single segment in the URL that matched the current route.
params
This property returns a Params object, which describes the URL parameters, indexed by name.
queryParams
This property returns a Params object, which describes the URL query parameters, indexed by name.
fragment
This property returns a string containing the URL fragment.

The snapshot exposes matched URL segments through url, named route parameters through params, query parameters through queryParams, and the URL fragment through fragment. section 24.3.1

Select a form mode from the URL

Define a route as { path: "form/:mode", component: FormComponent }. A URL ending in /form/edit assigns "edit" to mode. In the component constructor, `this.editing = activeRoute.snapshot.params["mode"] == "edit";` selects edit mode without requiring the component to inspect URL segment positions. section 24.3.2

For a component that only needs route values as properties, an alternative is to enable `bindToComponentInputs: true` in `RouterModule.forRoot` and receive values through input properties. That shifts parameter extraction to Angular; the form still converts the mode string into its boolean editing state. section 24.3.2

Decide whether a route may activate

A route guard receives an ActivatedRouteSnapshot as an argument. It can inspect `route.params["mode"]`: allow routes other than create immediately, but ask for agreement before activating create mode. Guards cannot receive routing parameters through component input properties; use the snapshot for route data there. section 26.2.2

How Pro Angular 16 covers it

How deep it goes
Defines the snapshot’s basic properties and works through parameter reads in components and guards, without detailing router internals.
Examples
Shows a create-or-edit form reading mode from a named route parameter and a guard checking that parameter before creation.
What it assumes
Assumes familiarity with Angular components and routes while introducing snapshot properties and named route parameters through examples.
What you can do after
Apply it via Angular Router to read route values in components and make parameter-dependent guard decisions.
How it connects
Connects ActivatedRoute to URL segments, route parameters, component input binding, and route guards.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro Angular 16. The section links point to the parts of the book it draws on.