Links & Route Events SPA+
An ordinary link navigates through the router, and a component reacts to a route change with a hook or a document event. For defining routes, see Route Management.
Links
Once the router is initialized, a click on an ordinary link to a path in the same site, such as <a href="/users/42">, navigates to that route without a page load. The link needs no special attribute or component, and a click on an icon or a <span> inside the link counts as a click on the link. The router calls navigate() with the link's path, query string, and hash, so guards and route events run as they do for router.navigate().
<nav>
<a href="/">Home</a>
<a href="/users/42">Profile</a>
<a href="/reports/2026.pdf" data-no-router>Annual report (PDF)</a>
<a href="https://example.com/help" target="_blank">Help</a>
</nav>
The router handles the first two links. The last two go to the browser, because the PDF link has data-no-router and the help link has target="_blank". The router leaves the click to the browser in these cases:
- The link has
data-no-router. Use it on a same-site link the router should not handle, such as a file or a page outside the app. - A modifier key (Cmd, Ctrl, Shift, or Alt) is held, so opening a link in a new tab or window works as usual.
- The link has a
targetother than_self, such astarget="_blank". - The link has a
downloadattribute. - The
hrefbegins with#, or is amailto:,tel:, orjavascript:link. - The
hrefis a full URL with a scheme (https://...) or begins with//, even when it points at the same site. - The link goes to another origin.
- An earlier handler has already called
preventDefault()on the click, including adata-actionwithdata-event-preventon the link. Neither the browser nor the router follows the link in that case.
A link that should navigate through the router must not have its click cancelled. In hash mode, a link written as <a href="#/products"> is one of the # links above: the browser changes the hash and the router follows the hash change.
Route Events
WildflowerJS provides two ways for components to react to route changes:
Option 1: onRouteChange Lifecycle Hook (Recommended)
Define an onRouteChange method in your component:
wildflower.component('my-component', {
state: {
currentPage: ''
},
// Called automatically when route changes
onRouteChange(to, from) {
console.log(`Navigated from ${from?.path} to ${to.path}`);
this.currentPage = to.path;
// Access route params and query
if (to.params.id) {
this.loadUser(to.params.id);
}
}
});
Option 2: addEventListener (For Advanced Use Cases)
For non-component code or when you need more control:
// Listen for navigation completion
document.addEventListener('route:afterChange', (e) => {
const { to, from } = e.detail;
console.log(`Navigated from ${from?.path} to ${to.path}`);
// Update UI, breadcrumbs, analytics, etc.
updateBreadcrumbs(to);
});
// Cancel navigation conditionally
document.addEventListener('route:beforeChange', (e) => {
if (hasUnsavedChanges()) {
e.preventDefault(); // Block navigation
showSavePrompt();
}
});
// Handle redirects
document.addEventListener('route:redirect', (e) => {
console.log(`Redirected from ${e.detail.from} to ${e.detail.to}`);
});
// Handle errors
document.addEventListener('route:error', (e) => {
console.error('Navigation error:', e.detail.error);
});
Available Events
| Event | When | Cancelable | Detail |
|---|---|---|---|
route:beforeChange |
Before navigation starts | Yes | { to, from } |
route:afterChange |
After navigation completes | No | { to, from } |
route:redirect |
When redirect occurs | No | { from, to } (paths) |
route:error |
When navigation fails | No | { error, to, from } |
route:afterChange and updates automatically!