Element-UI組件實現(xiàn)面包屑導航欄的示例代碼
面包屑導航欄是一種輔助導航系統(tǒng),它顯示用戶當前位置在網(wǎng)站或應用層次結(jié)構(gòu)中的位置,可以幫助用戶了解他們當前頁面的位置,并且可以方便地返回到上級頁面或首頁。
面包屑導航欄的實現(xiàn)原理:
路徑記錄與解析:
- 當用戶瀏覽網(wǎng)站時,面包屑導航記錄用戶經(jīng)過的路徑,通常是從主頁到當前頁面的一系列鏈接。
- 每當用戶訪問一個新的頁面時,面包屑導航會根據(jù)當前頁面的路徑信息更新顯示的內(nèi)容。
路由匹配:
- 在單頁應用(SPA)中,如使用 Vue.js 或 React 開發(fā)的應用,面包屑導航通常依賴于前端路由系統(tǒng)。
- 路由系統(tǒng)可以根據(jù)當前激活的路由來確定面包屑的結(jié)構(gòu)。例如,在 Vue Router 中,可以通過 matched 數(shù)組來獲取當前路由匹配的所有嵌套路由記錄。
動態(tài)生成面包屑項:
- 根據(jù)路由匹配的結(jié)果,動態(tài)生成面包屑項。每個面包屑項通常包含一個鏈接,指向該層級對應的頁面。
- 對于每個面包屑項,可以從路由的 meta 屬性中獲取必要的信息,如標題或圖標等。
響應式設計:
- 面包屑導航應當具有良好的響應式設計,以適應不同屏幕尺寸下的布局需求。
- 通常使用 CSS 來控制面包屑項的顯示方式,使其在窄屏設備上也能正確顯示。
用戶交互:
- 用戶可以通過點擊面包屑中的任意一項來快速返回到對應層級的頁面。
- 面包屑導航的最后一項通常是當前頁面,通常是不可點擊的。
使用 Element-UI 組件實現(xiàn)面包屑導航欄
安裝 Element-UI
npm install element-ui --save # 或者 yarn add element-ui
main.js 文件中引入并使用 Element-UI:
import Vue from 'vue'; import ElementUI from 'element-ui'; import 'element-ui/lib/theme-chalk/index.css'; Vue.use(ElementUI);
使用 組件
Element-UI 提供了 <el-breadcrumb> 組件來創(chuàng)建面包屑導航。你需要在 Vue 組件中定義一個數(shù)組來存儲面包屑的信息,并將這些信息展示在面包屑組件中。
示例代碼
定義路由示例:
export const routes = [
{
path: '/',
name: 'redirect',
component: Layout,
hidden: true, // 隱藏菜單
redirect: "/homePage", // 用戶在地址欄輸入 '/' 時會自動重定向到 /homePage 頁面
},
{
path: '/homePage',
component: Layout,
redirect: "/homePage/index",
meta: {
title: "首頁",
affix: true, // 固定路由,不跳轉(zhuǎn)
},
children: [
{
path: 'index',
name: 'homePageIndex',
meta: {
title: "首頁",
},
component: () => import('@/views/homePage/index.vue')
}
]
},
{
path: '/login',
component: () => import('@/views/login.vue'),
hidden: true
},
{
path: '/404',
component: () => import('@/views/error/404.vue'),
hidden: true
},
{
path: '/401',
component: () => import('@/views/error/401.vue'),
hidden: true
},
{
path: '/admin',
meta: {
title: "系統(tǒng)管理",
affix: true
},
component: Layout,
children: [
{
path: 'user',
name: 'userIndex',
meta: {
title: "用戶管理",
},
component: () => import('@/views/admin/user/index.vue')
},
{
path: 'role',
name: 'roleIndex',
meta: {
title: "角色管理",
},
component: () => import('@/views/admin/role/index.vue'),
children: [
{
path: 'add',
name: 'addRole',
hidden: true,
meta: {
title: "添加角色",
},
component: () => import('@/views/admin/user/index.vue')
},
{
path: 'update',
name: 'updateRole',
hidden: true,
meta: {
title: "編輯角色",
},
component: () => import('@/views/admin/role/index.vue')
}
]
}
]
}
]
這里定義了一級菜單系統(tǒng)管理,二級菜單用戶管理、角色管理,角色管理管理有兩個三級菜單添加角色和編輯角色,由于三級菜單路由配置了 hidden 屬性,所以左側(cè)菜單欄不展示。
頁面展示

在模板中使用 <el-breadcrumb> 組件:
<template>
<div class="tags_view">
<!-- 面包屑導航欄 -->
<el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
<el-breadcrumb-item v-for="item in Breadcrumb" :key="item.path" :to="toPath(item)" class="title">
{{ item.meta.title }}
</el-breadcrumb-item>
</el-breadcrumb>
</div>
</template>
<script>
export default {
name: 'TagsView',
data() {
return {
Breadcrumb: [],
}
},
created() {
this.genBreadcrumb(this.$route);
},
watch: {
$route() {
this.genBreadcrumb(this.$route);
},
},
methods: {
toPath(item) {
console.log('path::: ', item.meta);
// 如果路徑相同,點擊導航則不跳轉(zhuǎn)
if (item.path === this.$route.path) {
return ''
}
// 如果是固定路由,則不跳轉(zhuǎn)
if (item.meta.affix) {
return ''
} else {
// 跳轉(zhuǎn)路由
return item.path
}
},
genBreadcrumb(route) {
// console.log('route::: ', route);
// 獲取含有title屬性的路由。
let matched = route.matched.filter(
(item) => item.meta && item.meta.title
);
this.Breadcrumb = matched;
},
},
}
</script>
<style lang="scss" scoped>
.tags_view {
padding: 15px 0 0 15px;
}
.tags_view .title {
font-size: 16px !important;
}
</style>
- 通過監(jiān)聽路由,獲取所有含有title屬性的路由。
- 使用了 v-for 指令來遍歷
Breadcrumb數(shù)組,并為每一個面包屑項創(chuàng)建一個<el-breadcrumb-item>。 - 當點擊導航跳轉(zhuǎn)時判斷當前路由與跳轉(zhuǎn)的路由一樣時,則不進行路由跳轉(zhuǎn)。
- 通過路由配置里的
affix屬性判斷是否是固定路由,如果不是就進行路由跳轉(zhuǎn)
實現(xiàn)效果

左側(cè)菜單欄實現(xiàn)參考文章: Elemnt-UI + 遞歸組件實現(xiàn)后臺管理系統(tǒng)左側(cè)菜單
總結(jié)
通過監(jiān)聽路由變化動態(tài)生成面包屑項,并在組件創(chuàng)建時初始化面包屑。面包屑項的標題和路徑通過路由的 meta 屬性獲取,并根據(jù)當前路由路徑?jīng)Q定是否設置跳轉(zhuǎn)路徑。通過這種方式,組件能夠根據(jù)路由變化實時更新面包屑導航欄,提供清晰的導航指引。
到此這篇關于Element-UI組件實現(xiàn)面包屑導航欄的示例代碼的文章就介紹到這了,更多相關Element 面包屑導航欄內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
VUE3.2項目使用Echarts5.4詳細步驟總結(jié)
Vue3.2是一款非常流行的JavaScript框架,它讓在前端領域開發(fā)變得更加的便捷,下面這篇文章主要給大家介紹了關于VUE3.2項目使用Echarts5.4的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2023-06-06
Vue?報錯-4058?ENOENT:no?such?file?or?directory的原因及解決方法
Vue?報錯-4058?ENOENT:?no?such?file?or?directory的原因和解決辦法,關于為什么為會報這個錯誤,按照字面意思的理解就是沒有找到這個文件或這個路徑,說明是路徑不對,本文給大家分享解決方案,感興趣的朋友一起看看吧2023-10-10
Element?UI/Plus中全局修改el-table默認樣式的解決方案
element ui官方封裝好的el-table組件,好用是挺好用的,但不可避免的是默認的樣式,下面這篇文章主要給大家介紹了關于Element?UI/Plus中全局修改el-table默認樣式的解決方案,需要的朋友可以參考下2023-02-02
vue3純前端表格數(shù)據(jù)的導出與導入實現(xiàn)方式
這篇文章主要介紹了如何在純前端環(huán)境下使用xlsx-js-style庫進行Excel表格文件的下載,并自定義樣式,還提到了使用xlsx庫進行Excel表格數(shù)據(jù)的導入,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2025-01-01
vue鼠標hover(懸停)改變background-color移入變色問題
這篇文章主要介紹了vue鼠標hover(懸停)改變background-color移入變色問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10

