npm install vue-router@4
import { createRouter, createWebHistory } from 'vue-router'
import Home from '../views/Home.vue'
import User from '../views/User.vue'
const routes = [
{
path: '/',
name: 'Home',
component: Home
},
{
path: '/user/:id',
name: 'User',
component: User
}
]
const router = createRouter({
history: createWebHistory(),
routes
})
export default router
<template>
<div>
User {{ $route.params.id }}
</div>
</template>
<script>
export default {
name: 'User'
}
</script>
<template>
<div id="app">
<router-view />
</div>
</template>
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
createApp(App).use(router).mount('#app')
이제 /user/:id 경로로 이동하면 User 컴포넌트가 렌더링되고 동적으로 전달된 id 값이 표시된다.