使用 Vue Router 進(jìn)行路由定制和調(diào)用的完整示例
以下是一個使用 Vue Router 進(jìn)行路由定制和調(diào)用的完整示例,假設(shè)使用 Vue 3 和 Vue Router 4。
1. 創(chuàng)建項目并安裝依賴
首先,確保你已經(jīng)安裝了 Vue CLI。如果沒有安裝,可以通過以下命令安裝:
npm install -g @vue/cli
然后創(chuàng)建一個新的 Vue 項目:
vue create router - example cd router - example
接著安裝 Vue Router:
npm install vue - router@4
2. 定制路由(router/index.js)
在項目的 router 目錄下創(chuàng)建 index.js 文件,用于定義路由配置:
import { createRouter, createWebHistory } from 'vue - router';
import Home from '../views/Home.vue';
import About from '../views/About.vue';
import User from '../views/User.vue';
import UserDetail from '../views/UserDetail.vue';
const routes = [
{
path: '/',
name: 'Home',
component: Home
},
{
path: '/about',
name: 'About',
component: About
},
{
path: '/user',
name: 'User',
component: User,
children: [
{
path: ':id',
name: 'UserDetail',
component: UserDetail
}
]
}
];
const router = createRouter({
history: createWebHistory(),
routes
});
export default router;在上述代碼中:
- 使用
createWebHistory()選擇了 HTML5 歷史模式。 - 定義了三個主要路由:
Home、About和User。 User路由包含一個嵌套路由UserDetail,通過:id動態(tài)參數(shù)來匹配不同用戶的詳情頁。
3. 創(chuàng)建視圖組件
在 views 目錄下創(chuàng)建對應(yīng)的視圖組件:
Home.vue
<template>
<div>
<h1>首頁</h1>
</div>
</template>
<script setup>
</script>
<style scoped>
</style>About.vue
<template>
<div>
<h1>關(guān)于</h1>
</div>
</template>
<script setup>
</script>
<style scoped>
</style>User.vue
<template>
<div>
<h1>用戶列表</h1>
<router - view></router - view>
</div>
</template>
<script setup>
</script>
<style scoped>
</style>UserDetail.vue
<template>
<div>
<h1>用戶詳情 - {{ $route.params.id }}</h1>
</div>
</template>
<script setup>
</script>
<style scoped>
</style>4. 在主應(yīng)用中調(diào)用路由(main.js)
在 main.js 中引入并使用路由:
import { createApp } from 'vue';
import App from './App.vue';
import router from './router';
const app = createApp(App);
app.use(router);
app.mount('#app');5. 在應(yīng)用中使用路由(App.vue)
在 App.vue 中使用路由鏈接和路由視圖:
<template>
<div id="app">
<nav>
<ul>
<li><router - link to="/">首頁</router - link></li>
<li><router - link to="/about">關(guān)于</router - link></li>
<li><router - link to="/user">用戶</router - link></li>
</ul>
</nav>
<router - view></router - view>
</div>
</template>
<script setup>
</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: 60px;
}
</style>在上述 App.vue 中:
- 使用
<router - link>組件創(chuàng)建導(dǎo)航鏈接,點擊鏈接會觸發(fā)路由切換。 <router - view>組件用于渲染當(dāng)前匹配路由對應(yīng)的組件。
這樣,一個完整的 Vue 路由定制和調(diào)用的示例就完成了。用戶可以通過點擊導(dǎo)航鏈接在不同視圖之間切換,并且在 User 路由下,通過訪問 user/:id 形式的 URL 來查看不同用戶的詳情。
到此這篇關(guān)于使用 Vue Router 進(jìn)行路由定制和調(diào)用的示例的文章就介紹到這了,更多相關(guān)vue router路由定制與調(diào)用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue+EleMentUI實現(xiàn)el-table-colum表格select下拉框可編輯功能實例
這篇文章主要給大家介紹了關(guān)于Vue+EleMentUI實現(xiàn)el-table-colum表格select下拉框可編輯功能的相關(guān)資料,element-UI表格的使用相信大家都不陌生,文中給出了詳細(xì)的代碼示例,需要的朋友可以參考下2023-07-07
Vue3實現(xiàn)動態(tài)切換Menu的示例代碼
本文介紹了在Vue3項目中使用頂部導(dǎo)航欄和側(cè)邊欄,通過頂部導(dǎo)航控制側(cè)邊欄的生成,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-11-11
vue雙向數(shù)據(jù)綁定指令v-model的用法
這篇文章主要介紹了vue雙向數(shù)據(jù)綁定指令v-model的用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08

