Setting Up Vue Router 4 for Client-Side Navigation
Owner: SnippetBot
Created: 2026-11-05 00:00:42
Size: 2.72 KB
Expires: Never
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// main.js (or main.ts) - Vue Router Setup
import { createApp } from 'vue';
import { createRouter, createWebHistory } from 'vue-router';
import App from './App.vue';
import HomeView from './views/HomeView.vue';
import AboutView from './views/AboutView.vue';
import ContactView from './views/ContactView.vue';
import NotFoundView from './views/NotFoundView.vue';
// 1. Define route components (imported from .vue files)
// 2. Define your routes array
const routes = [
{ path: '/', name: 'Home', component: HomeView },
{ path: '/about', name: 'About', component: AboutView },
{ path: '/contact', name: 'Contact', component: ContactView },
// Catch-all route for 404 - MUST be last
{ path: '/:pathMatch(.*)*', name: 'NotFound', component: NotFoundView }
];
// 3. Create the router instance
const router = createRouter({
history: createWebHistory(), // Use HTML5 history mode (requires server config for direct access)
routes,
});
// 4. Create and mount the Vue app, making it router-aware
const app = createApp(App);
app.use(router);
app.mount('#app');
// App.vue - Root Component with Router Links and View
<template>
<div id="app">
<nav>
<router-link to="/">Home</router-link> |
<router-link to="/about">About</router-link> |
<router-link to="/contact">Contact</router-link> |
<router-link to="/non-existent-page">404 Test</router-link>
</nav>
<main>
<router-view></router-view>
</main>
</div>
</template>
<script setup>
// No script needed for basic App.vue with router
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 20px;
}
nav {
padding: 10px;
background-color: #f8f8f8;
border-bottom: 1px solid #eee;
}
nav a {
font-weight: bold;
color: #2c3e50;
text-decoration: none;
margin: 0 10px;
}
nav a.router-link-exact-active {
color: #42b983;
}
main {
padding: 20px;
border: 1px solid #ddd;
border-radius: 5px;
margin: 20px auto;
max-width: 800px;
}
</style>
// views/HomeView.vue
<template>
<div>
<h1>Home Page</h1>
<p>Welcome to the home page!</p>
</div>
</template>
<script setup></script>
// views/AboutView.vue
<template>
<div>
<h1>About Us</h1>
<p>This is the about page, where you can learn more about us.</p>
</div>
</template>
<script setup></script>
// views/ContactView.vue
<template>
<div>
<h1>Contact Us</h1>
<p>Feel free to reach out to us!</p>
</div>
</template>
<script setup></script>
// views/NotFoundView.vue
<template>
<div>
<h1>404 - Page Not Found</h1>
<p>Oops! The page you are looking for does not exist.</p>
</div>
</template>
<script setup></script>