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
