Router
Class for centralized routing management.
Provides a common interface for working with history, location, and route collections.
Constructor
ts
newRouter(config: RouterConfiguration)
Accepts configuration with route collection and routing settings.
Basic example
ts
import{createBrowserHistory,QueryParams,Route,routeConfig,RouteGroup,Router,}from'mobx-route';consthistory=createBrowserHistory();constqueryParams=newQueryParams({ history });routeConfig.set({history,queryParams,baseUrl:'/base-url',});exportconstroutes={home:newRoute('/'),projects:newRouteGroup({index:newRoute('/projects', { index:true}),new:newRoute('/projects/new'),details:newRoute('/projects/:projectId'),}),};exportconstrouter=newRouter({routes,history,location,queryParams,});router.routes.home.open();router.navigate(router.router.home.createUrl());router.history.back();
Methods and properties
routes: TRoutesCollection
Root collection of application routes. Can contain nestedRouteGroups
.
Example:
ts
router.routes.home.open();router.routes.admin.routes.dashboard.isOpened;
history: AnyHistory
Interface for managing browser history frommobx-location-history
package.
Handles navigation operations.
Example:
ts
router.history.back();
location: AnyLocation
Reactive object with browser location frommobx-location-history
package.
Example:
ts
autorun(()=>{console.log('Current path:', router.location.pathname);});
query: IQueryParams
Interface for managing query parameters frommobx-location-history
package.
Automatically synchronized with current url.
Example:
ts
router.query.data;// { q: 'test' }router.query.update({ bar:1});router.query.data;// { q: 'test', bar: '1' }
navigate(url: string, options?): void
action
Universal method for URL navigation.
Examples:
ts
// Basic navigationrouter.navigate('/about');// With query parametersrouter.navigate('/search', {query: { q:'test'},replace:true});// Using generated URLconsturl=router.routes.profile.createUrl({ userId:42});router.navigate(url);