Programmatic Navigation
Aside from using <RouterLink> to create anchor tags for declarative navigation, we can do this programmatically using the router's instance methods.
router.push
To navigate to a different URL, use router.push. This method adds a new entry to the history stack, so when the user clicks the browser back button, they'll go back to the previous URL.
The method returns a Promise<Route> that resolves to the new route after navigation completes (including all guards):
router.replace
Acts like router.push but does not add a new history entry. It replaces the current entry instead:
This is useful when you want to redirect without cluttering the browser history — for example, after form submission or login:
Navigation with Objects
Both push and replace accept a RouteLocationInput, which can be a string or an object with these properties:
path:string— The target pathquery:Record<string, string>— Query parametershash:string— Hash fragment (e.g.,'#section')state:Record<string, unknown>— State stored inhistory.state(not visible in URL)params:Record<string, string>— Dynamic segment valueskeepScrollPosition:boolean— Iftrue, don't scroll to top after navigationstatusCode:number— HTTP status code (useful for SSR)
Using params
The params option lets you pass dynamic segment values that are applied to the matched route's path pattern:
Using state
The state property stores data in history.state. Unlike query params, state is not visible in the URL and is preserved across forward/back navigation:
Window Navigation
Standard push/replace perform SPA navigation — the page doesn't reload, only the routed content changes. Window navigation methods trigger a full browser navigation instead.
router.pushWindow
Opens the target in a new browser tab/window (equivalent to window.open):
router.replaceWindow
Navigates the current tab to a new URL (equivalent to window.location.replace):
When to Use Window Navigation
- Navigate within your SPA: use
push/replace - Navigate to a different micro-frontend: use
pushWindow/replaceWindow - Open in new tab: use
pushWindow - Full page reload / redirect to external URL: use
replaceWindow - Navigate to a page outside router scope: use
pushWindow/replaceWindow
Guard Pipeline Differences
Window navigation methods skip most of the guard pipeline since the browser will perform a full navigation anyway:
History Navigation
These methods mirror the browser's native history navigation:
router.back()
Go back one step in history. Equivalent to router.go(-1):
Returns Promise<Route | null>. Returns null if there's no history to go back to (the user is at the start of their session).
router.forward()
Go forward one step. Equivalent to router.go(1):
Returns Promise<Route | null>. Returns null if there's no forward history.
router.go(n)
Move n steps in history. Positive values go forward, negative values go back:
Returns Promise<Route | null>. Returns null if the target position doesn't exist in history. Note that router.go(0) returns null immediately without any action (unlike location.reload()).
router.restartApp
Remounts the current micro-app without changing the URL. This is useful when you need to reset the application state completely:
You can optionally pass a new route location:
This method runs the full guard pipeline (excluding override), unmounts the current micro-app, and remounts it fresh.
router.resolve
Resolves a route location without actually navigating. This is useful for generating URLs, checking if a route exists, or inspecting what a navigation would produce:
Use it to generate link URLs without triggering navigation:
The keepScrollPosition Option
By default, push and replace scroll the page to the top. Pass keepScrollPosition: true to prevent this:
See Scroll Behavior for full details on how scrolling works.
Error Handling
All navigation methods can throw errors. Always handle them appropriately:
See Error Handling for more details.