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

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

實(shí)現(xiàn)面包屑代碼
<template>
// 面包屑組件, separator為分割線,具體可以去elementPlus官網(wǎng)查看
<el-breadcrumb separator="/">
<div class="breadcrumb-content">
// 動(dòng)畫組件 [官網(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);
});
// 判斷是否存在首頁(yè)默認(rèn)數(shù)據(jù),不存在就插入到數(shù)據(jù)首位
if (!arr.breadCrumbList.length || arr.breadCrumbList[0].name !== "Dashboard")
arr.breadCrumbList.unshift({
path: "/dashboard/index",
meta: { title: "首頁(yè)" },
});
};
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實(shí)現(xiàn)面包屑功能的文章就介紹到這了,更多相關(guān)vue面包屑內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vuejs在v-for中,利用index來(lái)對(duì)第一項(xiàng)添加class的方法
下面小編就為大家分享一篇Vuejs在v-for中,利用index來(lái)對(duì)第一項(xiàng)添加class的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
vue2實(shí)現(xiàn)provide inject傳遞響應(yīng)式
在看element-ui的源碼的時(shí)候,注意到源碼里面有很多地方使用provide和inject的屬性,本文主要介紹了vue2實(shí)現(xiàn)provide inject傳遞響應(yīng)式,分享給大家,感興趣的可以了解一下2021-05-05
vue 解決form表單提交但不跳轉(zhuǎn)頁(yè)面的問(wèn)題
今天小編就為大家分享一篇vue 解決form表單提交但不跳轉(zhuǎn)頁(yè)面的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10
antdv vue upload自定義上傳結(jié)合表單提交方式
這篇文章主要介紹了antdv vue upload自定義上傳結(jié)合表單提交方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
Vue3+Vant打包報(bào)錯(cuò) Identifier ‘bem‘ has alrea
在實(shí)際項(xiàng)目開發(fā)中,前端構(gòu)建的坑經(jīng)常出現(xiàn)在一些意想不到的地方,這次就出現(xiàn)了Vue3+Vant打包時(shí)報(bào)錯(cuò)Identifier ‘bem‘ has already been declared,下面我們就來(lái)看看具體解決方法吧2025-09-09
Vue項(xiàng)目實(shí)現(xiàn)簡(jiǎn)單的權(quán)限控制管理功能
這篇文章主要介紹了Vue項(xiàng)目實(shí)現(xiàn)簡(jiǎn)單的權(quán)限控制功能,文中給大家介紹了兩種方式進(jìn)行權(quán)限限制,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2019-07-07

