Vue3+Elementplus實現(xiàn)面包屑功能
Vue3+Elementplus引入面包屑功能總結(jié)

面包屑(Breadcrumb)是網(wǎng)站或應(yīng)用程序界面中的一種導(dǎo)航輔助工具,通常以層次結(jié)構(gòu)顯示用戶當(dāng)前所在位置的路徑。Element Plus 是一個基于 Vue.js 的 UI 組件庫,在實現(xiàn)面包屑功能時,可以通過 Element Plus 提供的 Breadcrumb 組件來實現(xiàn)。
在 Element Plus 中使用面包屑功能,首先需要安裝并引入 Element Plus 的相關(guān)組件。然后,可以通過以下步驟來實現(xiàn)面包屑功能 正菜來了
路由內(nèi)的內(nèi)容
因為面包屑是根據(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: "職位管理",
},
},
]
}
}開始插入面包屑
溫馨提醒:這個有點仔細,請仔細看下去
<!-- 面包屑(放到你想要放的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的元素,因為需要用title填充面包屑的內(nèi)容
let getMatched=()=>{
console.log(route.matched); //打印路徑數(shù)組
breadList.value = route.matched.filter(item => item.meta && item.meta.title);
}
第 3 步:頁面加載時調(diào)用獲取路徑形成面包屑
onMounted(()=>{
getMatched();
})
第 4 步 :監(jiān)聽路由發(fā)生變化面包屑進行相應(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 組件:面包屑的項組件,用于表示每個導(dǎo)航路徑的一部分??梢酝ㄟ^插槽(slot)的方式設(shè)置每個項的內(nèi)容,并通過
to屬性設(shè)置項的鏈接地址。 - 面包屑的數(shù)據(jù)源:通常使用一個數(shù)組來存儲面包屑的導(dǎo)航路徑信息。每個導(dǎo)航路徑都是一個對象,包含兩個屬性:
text表示項的文本內(nèi)容,to表示項的鏈接地址。 - 動態(tài)生成面包屑:根據(jù)頁面的路由信息或其他需要顯示的路徑信息,動態(tài)生成面包屑的數(shù)據(jù)源,然后通過 v-for 指令遍歷數(shù)據(jù)源,動態(tài)生成 BreadcrumbItem 組件。
- 設(shè)置當(dāng)前項:根據(jù)頁面的當(dāng)前路由信息,在對應(yīng)的 BreadcrumbItem 組件上添加一個標(biāo)識,表示當(dāng)前所在位置。
通過以上組件和方法的組合使用,可以實現(xiàn)基本的面包屑功能。
以上就是Vue3+Elementplus實現(xiàn)面包屑功能的詳細內(nèi)容,更多關(guān)于Vue3 Elementplus面包屑的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Vue中?引入使用?babel-polyfill?兼容低版本瀏覽器的方法
最近在項目中使用 webpack 打包后升級,用戶反饋使用瀏覽器(chrome 45)訪問白屏。經(jīng)過排查發(fā)現(xiàn):由于 chrome 45 無法兼容 ES6 語法導(dǎo)致的,接下來給大家介紹下Vue中?引入使用?babel-polyfill?兼容低版本瀏覽器方法,需要的朋友可以參考下2023-02-02
vite添加環(huán)境變量import.meta.env方式
Vite通過`import.meta.env`對象暴露環(huán)境變量,可以在不同文件中配置不同環(huán)境下的變量,便于維護和使用,配置環(huán)境變量和模式時,需要注意在字符串中使用`import.meta.env.VITE_APP_TITLE`的方式,避免打包報錯2026-01-01
vue新玩法VueUse工具庫具體用法@vueuse/core詳解
這篇文章主要介紹了vue新玩法VueUse-工具庫@vueuse/core,VueUse不是Vue.use,它是一個基于?Composition?API?的實用函數(shù)集合,下面是具體的一些用法,需要的朋友可以參考下2022-08-08

