In the browser, the location (URL information) and session history (a stack of locations visited by the current browser tab) are stored in the global window object. They are accessible via:
Our location data is a dynamic and essential part of the application state-the kind of state that belongs in a store. Holding it in the store enables devtools luxuries like time-travel debugging, and easy access from any store-connected component.
This plugin binds that state from the Angular router to our NGXS store.
Installation
npmi@ngxs/router-plugin# or if you are using yarnyarnadd@ngxs/router-plugin# or if you are using pnpmpnpmi@ngxs/router-plugin
Usage
When calling provideStore, include withNgxsRouterPlugin in your app config:
Now the route will be reflected in your store under the router state name. The state is represented as a RouterStateSnapshot object.
You can also navigate using the store's dispatch method. It accepts the following arguments: new Navigate(path: any[], queryParams?: Params, extras?: NavigationExtras). A simple example would be navigating to the admin page like this:
You can use action handlers to listen to state changes in your components and services by subscribing to the RouterNavigation, RouterCancel, RouterError or RouterDataResolved action classes.
Listening to the data resolution event
You can listen for the RouterDataResolved action, which is dispatched when the navigated route has linked resolvers. For example:
The RouterNavigation action is dispatched before guards and resolvers are run by default. Therefore the action handler may run too soon due to a navigation cancel by any guard or resolver. The RouterNavigation action may be run after all guards and resolvers by providing the navigationActionTiming configuration property: