JAVASCRIPT
Vue Router 4: Basic Setup and Programmatic Navigation
Learn to set up Vue Router 4 for Single Page Applications, define routes with parameters, and perform programmatic navigation using `useRouter`.
<!-- main.js (or main.ts) -->
import { createApp } from 'vue';
import App from './App.vue';
import router from './router'; // Import the router instance
const app = createApp(App);
app.use(router); // Use the router plugin
app.mount('#app');
<!-- router/index.js -->
import { createRouter, createWebHistory } from 'vue-router';
import Home from '../views/Home.vue';
import About from '../views/About.vue';
import UserProfile from '../views/UserProfile.vue';
const routes = [
{
path: '/',
name: 'Home',
component: Home
},
{
path: '/about',
name: 'About',
component: About
},
{
path: '/user/:id',
name: 'UserProfile',
component: UserProfile,
props: true // Pass route params as props to the component
},
{
path: '/dashboard',
name: 'Dashboard',
// Route for programmatic navigation example
component: () => import('../views/Dashboard.vue') // Lazy load
}
];
const router = createRouter({
history: createWebHistory(),
routes
});
export default router;
<!-- App.vue -->
<template>
<nav>
<router-link to="/">Home</router-link> |
<router-link to="/about">About</router-link> |
<router-link to="/user/123">User 123</router-link> |
<router-link to="/dashboard">Dashboard</router-link>
</nav>
<router-view />
</template>
<script setup>
// No script logic needed for App.vue in this example
</script>
<!-- views/Home.vue -->
<template>
<div>
<h2>Home View</h2>
<p>Welcome to the home page!</p>
</div>
</template>
<script setup>
// ...
</script>
<!-- views/About.vue -->
<template>
<div>
<h2>About View</h2>
<p>This is the about page.</p>
</div>
</template>
<script setup>
// ...
</script>
<!-- views/UserProfile.vue -->
<template>
<div>
<h2>User Profile</h2>
<p>User ID: {{ id }}</p>
<button @click="goToHome">Go to Home Programmatically</button>
</div>
</template>
<script setup>
import { useRouter, useRoute } from 'vue-router';
import { defineProps } from 'vue';
// Access route parameters via props (if props: true is set in route config)
const props = defineProps({
id: String
});
// Access router instance for programmatic navigation
const router = useRouter();
const goToHome = () => {
router.push('/'); // Navigate to home route
};
// Also possible to access route info directly
const route = useRoute();
console.log('Current route name:', route.name);
</script>
<!-- views/Dashboard.vue -->
<template>
<div>
<h2>Dashboard View</h2>
<p>Welcome to your dashboard!</p>
<button @click="goToUserProgrammatically">Go to User 456</button>
</div>
</template>
<script setup>
import { useRouter } from 'vue-router';
const router = useRouter();
const goToUserProgrammatically = () => {
router.push({ name: 'UserProfile', params: { id: '456' } });
};
</script>
How it works: This snippet provides a comprehensive guide to setting up and using Vue Router 4 in a Vue 3 application. It covers initial router configuration in `main.js`, defining routes with `createRouter` and `createWebHistory`, including dynamic routes with parameters (`/user/:id`). The `router-link` component facilitates declarative navigation, while `router-view` renders the matched component. For programmatic navigation, `useRouter` provides access to methods like `router.push()` to change routes, and `useRoute` allows components to access current route information like parameters and name.