SyntaxStudy
Sign Up
Vue.js RouterLink, RouterView and Programmatic Navigation
Vue.js Beginner 1 min read

RouterLink, RouterView and Programmatic Navigation

`` renders an `` tag that navigates without a full page reload. Vue Router automatically adds `router-link-active` and `router-link-exact-active` CSS classes when the link matches the current route, making it trivial to style active navigation items. The `to` prop accepts a string path or a location object with `name`, `params`, and `query` properties. For programmatic navigation — redirecting after form submission, guarding access in a lifecycle hook — inject the router with `useRouter()`. The returned router instance exposes `push(location)` for forward navigation, `replace(location)` to navigate without adding a history entry, `back()`, `forward()`, and `go(n)`. All methods return a Promise that resolves when navigation completes. `useRoute()` returns the current reactive route object, exposing `params`, `query`, `hash`, `name`, `path`, and `meta`. Because it's reactive, a `watch(() => route.params.id, ...)` in a detail page automatically re-fetches data when the user navigates from one detail to another without the component being destroyed and recreated.