Vue3+Elementplus實(shí)現(xiàn)面包屑功能
Vue3+Elementplus引入面包屑功能總結(jié)
面包屑(Breadcrumb)是網(wǎng)站或應(yīng)用程序界面中的一種導(dǎo)航輔助工具,通常以層次結(jié)構(gòu)顯示用戶當(dāng)前所在位置的路徑。Element Plus 是一個(gè)基于 Vue.js 的 UI 組件庫,在實(shí)現(xiàn)面包屑功能時(shí),可以通過 Element Plus 提供的 Breadcrumb 組件來實(shí)現(xiàn)。
在 Element Plus 中使用面包屑功能,首先需要安裝并引入 Element Plus 的相關(guān)組件。然后,可以通過以下步驟來實(shí)現(xiàn)面包屑功能 正菜來了
路由內(nèi)的內(nèi)容
因?yàn)槊姘际歉鶕?jù)路由的內(nèi)容來顯示的
{ path: "/home", name: "home", // 懶加載 component: () => import("../views/home/index.vue"), meta: { title: "主頁", }, children: [ { path: "/recruitManage", name: "recruitManage", component: () => import("../views/home/childrens/RecruitManage.vue"), meta: { title: "招聘管理", icon: Guide }, children: [ { path: "/noticeList", name: "noticeList", // 懶加載 component: () => import("../views/home/childrens/NoticeList.vue"), meta: { title: "公告管理" }, }, { path: "/postList", name: "postList", // 懶加載 component: () => import("../views/home/childrens/PostList.vue"), meta: { title: "職位管理", }, }, ] } }
開始插入面包屑
溫馨提醒:這個(gè)有點(diǎn)仔細(xì),請仔細(xì)看下去
<!-- 面包屑(放到你想要放的template中的位置) --> <el-breadcrumb separator=">"> <!-- <el-breadcrumb-item :to="{ path: '/home' }">首頁</el-breadcrumb-item> --> <template v-for="(item, index) in breadList"> <el-breadcrumb-item v-if="item.name" :key="index" :to="item.path" >{{ item.meta.title }}</el-breadcrumb-item> </template> </el-breadcrumb>
<script setup> import { useRouter,useRoute } from 'vue-router'; let router = useRouter(); let route = useRoute(); let getMatched=()=>{ console.log(route.matched); breadList.value = route.matched.filter(item => item.meta && item.meta.title); } onMounted(()=>{ getMatched(); }) watch(() => route.path, (newValue, oldValue) => { //監(jiān)聽路由路徑是否發(fā)生變化,之后更改面包屑 breadList.value = route.matched.filter(item => item.meta && item.meta.title); }) </script>
插入內(nèi)容講解
第 1 步:導(dǎo)入route,使用其能訪問到路由的路徑
import { useRouter,useRoute } from 'vue-router'; let router = useRouter(); let route = useRoute();
第 2 步 :編寫獲取路徑的方法 matched獲取訪問網(wǎng)址在路由中的路徑,并過濾掉item沒有title的元素,因?yàn)樾枰胻itle填充面包屑的內(nèi)容
let getMatched=()=>{ console.log(route.matched); //打印路徑數(shù)組 breadList.value = route.matched.filter(item => item.meta && item.meta.title); }
第 3 步:頁面加載時(shí)調(diào)用獲取路徑形成面包屑
onMounted(()=>{ getMatched(); })
第 4 步 :監(jiān)聽路由發(fā)生變化面包屑進(jìn)行相應(yīng)的修改
watch(() => route.path, (newValue, oldValue) => { //監(jiān)聽路由路徑是否發(fā)生變化,之后更改面包屑 breadList.value = route.matched.filter(item => item.meta && item.meta.title); })
面包屑引入過程梳理
在 Element Plus 中,面包屑功能主要涉及以下組件和方法:
- Breadcrumb 組件:面包屑的容器組件,用于包裹 BreadcrumbItem 組件??梢酝ㄟ^
separator
屬性設(shè)置面包屑分隔符,默認(rèn)為/
。 - BreadcrumbItem 組件:面包屑的項(xiàng)組件,用于表示每個(gè)導(dǎo)航路徑的一部分。可以通過插槽(slot)的方式設(shè)置每個(gè)項(xiàng)的內(nèi)容,并通過
to
屬性設(shè)置項(xiàng)的鏈接地址。 - 面包屑的數(shù)據(jù)源:通常使用一個(gè)數(shù)組來存儲面包屑的導(dǎo)航路徑信息。每個(gè)導(dǎo)航路徑都是一個(gè)對象,包含兩個(gè)屬性:
text
表示項(xiàng)的文本內(nèi)容,to
表示項(xiàng)的鏈接地址。 - 動態(tài)生成面包屑:根據(jù)頁面的路由信息或其他需要顯示的路徑信息,動態(tài)生成面包屑的數(shù)據(jù)源,然后通過 v-for 指令遍歷數(shù)據(jù)源,動態(tài)生成 BreadcrumbItem 組件。
- 設(shè)置當(dāng)前項(xiàng):根據(jù)頁面的當(dāng)前路由信息,在對應(yīng)的 BreadcrumbItem 組件上添加一個(gè)標(biāo)識,表示當(dāng)前所在位置。
通過以上組件和方法的組合使用,可以實(shí)現(xiàn)基本的面包屑功能。
以上就是Vue3+Elementplus實(shí)現(xiàn)面包屑功能的詳細(xì)內(nèi)容,更多關(guān)于Vue3 Elementplus面包屑的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue2.0使用v-for循環(huán)制作多級嵌套菜單欄
這篇文章主要介紹了vue2.0制作多級嵌套菜單欄,主要使用v-for循環(huán)生成一個(gè)多級嵌套菜單欄,這個(gè)方法應(yīng)用非常廣泛,需要的朋友可以參考下2018-06-06對Vue- 動態(tài)元素屬性及v-bind和v-model的區(qū)別詳解
今天小編就為大家分享一篇對Vue- 動態(tài)元素屬性及v-bind和v-model的區(qū)別詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08vue+elementUI實(shí)現(xiàn)表格關(guān)鍵字篩選高亮
這篇文章主要為大家詳細(xì)介紹了vue+elementUI實(shí)現(xiàn)表格關(guān)鍵字篩選高亮,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05解決vue單頁面多個(gè)組件嵌套監(jiān)聽瀏覽器窗口變化問題
這篇文章主要介紹了解決vue單頁面多個(gè)組件嵌套監(jiān)聽瀏覽器窗口變化問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07vue里面v-bind和Props 利用props綁定動態(tài)數(shù)據(jù)的方法
今天小編就為大家分享一篇vue里面v-bind和Props 利用props綁定動態(tài)數(shù)據(jù)的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08