Vue基于el-breadcrumb實現(xiàn)面包屑功能(操作代碼)
vue3 + elementPlus 實現(xiàn)面包屑功能
文章后面附效果圖
數(shù)據(jù)結(jié)構(gòu)
首先展示一下數(shù)據(jù)基礎(chǔ)結(jié)構(gòu)
紅色框框是默認存在的數(shù)據(jù),其他數(shù)據(jù)就是通過選中側(cè)邊欄菜單進行數(shù)據(jù)插入
關(guān)鍵數(shù)據(jù)字段為 path, meta

準(zhǔn)備側(cè)邊欄
首先需要自己準(zhǔn)備一個側(cè)邊欄這邊就不進行講解,上個效果圖

實現(xiàn)面包屑代碼
<template>
// 面包屑組件, separator為分割線,具體可以去elementPlus官網(wǎng)查看
<el-breadcrumb separator="/">
<div class="breadcrumb-content">
// 動畫組件 [官網(wǎng)鏈接](https://cn.vuejs.org/guide/built-ins/transition.html)
<transition-group name="breadcrumb">
<el-breadcrumb-item class="breadcrumb-item" v-for="item in arr.breadCrumbList" :key="item.path">
<span v-if="item.redirect === route.path">
{{ item.meta.title }}
</span>
<a v-else @click="handleLine(item)">{{ item.meta.title }}</a>
</el-breadcrumb-item>
</transition-group>
</div>
</el-breadcrumb>
</template>
<script setup lang="ts">
const router = useRouter();
// 面包屑數(shù)據(jù)
let arr = reactive({
breadCrumbList: [],
});
const route = useRoute();
// 獲取面包屑數(shù)據(jù)
const getBreadcrumbList = () => {
arr.breadCrumbList = [];
route.matched.forEach((item) => {
if (item.meta.title) arr.breadCrumbList.push(item);
});
// 判斷是否存在首頁默認數(shù)據(jù),不存在就插入到數(shù)據(jù)首位
if (!arr.breadCrumbList.length || arr.breadCrumbList[0].name !== "Dashboard")
arr.breadCrumbList.unshift({
path: "/dashboard/index",
meta: { title: "首頁" },
});
};
getBreadcrumbList();
// 監(jiān)聽路由變化
watch(route, () => {
getBreadcrumbList();
});
// 面包屑跳轉(zhuǎn)
const handleLine = ({ redirect, path }) => {
redirect ? router.push(redirect) : router.push(path);
};
</script>
<style lang="less" scoped>
.el-breadcrumb {
display: flex;
padding-left: 10px;
line-height: 50px;
background-color: #f6f6f6;
}
.breadcrumb-item {
margin-left: 8px;
}
.breadcrumb-content {
position: relative;
}
.breadcrumb-enter-active,
.breadcrumb-leave-active {
transition: all 0.5s;
}
.breadcrumb-enter-from,
.breadcrumb-leave-active {
opacity: 0;
}
.breadcrumb-move {
transition: all 0.5s;
}
.breadcrumb-leave-active {
position: absolute;
right: -50px;
}
</style>效果圖

到此這篇關(guān)于Vue基于el-breadcrumb實現(xiàn)面包屑功能的文章就介紹到這了,更多相關(guān)vue面包屑內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue實現(xiàn)導(dǎo)出word文檔功能實例(含多張圖片)
項目需要導(dǎo)出word,于是乎又是查閱資料,然后自己寫,下面這篇文章主要給大家介紹了關(guān)于vue實現(xiàn)導(dǎo)出word文檔功能(含多張圖片)的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-09-09
vuex(vue狀態(tài)管理)的特殊應(yīng)用案例分享
Vuex 是一個專為 Vue.js 應(yīng)用程序開發(fā)的狀態(tài)管理模式。它采用集中式存儲管理應(yīng)用的所有組件的狀態(tài),并以相應(yīng)的規(guī)則保證狀態(tài)以一種可預(yù)測的方式發(fā)生變化。2020-03-03
Vite3 Svelte3構(gòu)建Web應(yīng)用報錯process is not def
這篇文章主要介紹了Vite3 Svelte3構(gòu)建Web應(yīng)用報錯:'process is not defined'解決方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06
vue使用?vue-socket.io三種方式及踩坑實例解析
這篇文章主要為大家介紹了vue使用?vue-socket.io三種方式及踩坑實例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09

