Element-UI組件實(shí)現(xiàn)面包屑導(dǎo)航欄的示例代碼
面包屑導(dǎo)航欄是一種輔助導(dǎo)航系統(tǒng),它顯示用戶當(dāng)前位置在網(wǎng)站或應(yīng)用層次結(jié)構(gòu)中的位置,可以幫助用戶了解他們當(dāng)前頁面的位置,并且可以方便地返回到上級頁面或首頁。
面包屑導(dǎo)航欄的實(shí)現(xiàn)原理:
路徑記錄與解析:
- 當(dāng)用戶瀏覽網(wǎng)站時,面包屑導(dǎo)航記錄用戶經(jīng)過的路徑,通常是從主頁到當(dāng)前頁面的一系列鏈接。
- 每當(dāng)用戶訪問一個新的頁面時,面包屑導(dǎo)航會根據(jù)當(dāng)前頁面的路徑信息更新顯示的內(nèi)容。
路由匹配:
- 在單頁應(yīng)用(SPA)中,如使用 Vue.js 或 React 開發(fā)的應(yīng)用,面包屑導(dǎo)航通常依賴于前端路由系統(tǒng)。
- 路由系統(tǒng)可以根據(jù)當(dāng)前激活的路由來確定面包屑的結(jié)構(gòu)。例如,在 Vue Router 中,可以通過 matched 數(shù)組來獲取當(dāng)前路由匹配的所有嵌套路由記錄。
動態(tài)生成面包屑項(xiàng):
- 根據(jù)路由匹配的結(jié)果,動態(tài)生成面包屑項(xiàng)。每個面包屑項(xiàng)通常包含一個鏈接,指向該層級對應(yīng)的頁面。
- 對于每個面包屑項(xiàng),可以從路由的 meta 屬性中獲取必要的信息,如標(biāo)題或圖標(biāo)等。
響應(yīng)式設(shè)計:
- 面包屑導(dǎo)航應(yīng)當(dāng)具有良好的響應(yīng)式設(shè)計,以適應(yīng)不同屏幕尺寸下的布局需求。
- 通常使用 CSS 來控制面包屑項(xiàng)的顯示方式,使其在窄屏設(shè)備上也能正確顯示。
用戶交互:
- 用戶可以通過點(diǎn)擊面包屑中的任意一項(xiàng)來快速返回到對應(yīng)層級的頁面。
- 面包屑導(dǎo)航的最后一項(xiàng)通常是當(dāng)前頁面,通常是不可點(diǎn)擊的。
使用 Element-UI 組件實(shí)現(xiàn)面包屑導(dǎo)航欄
安裝 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)建面包屑導(dǎo)航。你需要在 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"> <!-- 面包屑導(dǎo)航欄 --> <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); // 如果路徑相同,點(diǎn)擊導(dǎo)航則不跳轉(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ù)組,并為每一個面包屑項(xiàng)創(chuàng)建一個<el-breadcrumb-item>
。 - 當(dāng)點(diǎn)擊導(dǎo)航跳轉(zhuǎn)時判斷當(dāng)前路由與跳轉(zhuǎn)的路由一樣時,則不進(jìn)行路由跳轉(zhuǎn)。
- 通過路由配置里的
affix
屬性判斷是否是固定路由,如果不是就進(jìn)行路由跳轉(zhuǎn)
實(shí)現(xiàn)效果
左側(cè)菜單欄實(shí)現(xiàn)參考文章: Elemnt-UI + 遞歸組件實(shí)現(xiàn)后臺管理系統(tǒng)左側(cè)菜單
總結(jié)
通過監(jiān)聽路由變化動態(tài)生成面包屑項(xiàng),并在組件創(chuàng)建時初始化面包屑。面包屑項(xiàng)的標(biāo)題和路徑通過路由的 meta
屬性獲取,并根據(jù)當(dāng)前路由路徑?jīng)Q定是否設(shè)置跳轉(zhuǎn)路徑。通過這種方式,組件能夠根據(jù)路由變化實(shí)時更新面包屑導(dǎo)航欄,提供清晰的導(dǎo)航指引。
到此這篇關(guān)于Element-UI組件實(shí)現(xiàn)面包屑導(dǎo)航欄的示例代碼的文章就介紹到這了,更多相關(guān)Element 面包屑導(dǎo)航欄內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
VUE3.2項(xiàng)目使用Echarts5.4詳細(xì)步驟總結(jié)
Vue3.2是一款非常流行的JavaScript框架,它讓在前端領(lǐng)域開發(fā)變得更加的便捷,下面這篇文章主要給大家介紹了關(guān)于VUE3.2項(xiàng)目使用Echarts5.4的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06Vue?報錯-4058?ENOENT:no?such?file?or?directory的原因及解決方法
Vue?報錯-4058?ENOENT:?no?such?file?or?directory的原因和解決辦法,關(guān)于為什么為會報這個錯誤,按照字面意思的理解就是沒有找到這個文件或這個路徑,說明是路徑不對,本文給大家分享解決方案,感興趣的朋友一起看看吧2023-10-10Element?UI/Plus中全局修改el-table默認(rèn)樣式的解決方案
element ui官方封裝好的el-table組件,好用是挺好用的,但不可避免的是默認(rèn)的樣式,下面這篇文章主要給大家介紹了關(guān)于Element?UI/Plus中全局修改el-table默認(rèn)樣式的解決方案,需要的朋友可以參考下2023-02-02vue3純前端表格數(shù)據(jù)的導(dǎo)出與導(dǎo)入實(shí)現(xiàn)方式
這篇文章主要介紹了如何在純前端環(huán)境下使用xlsx-js-style庫進(jìn)行Excel表格文件的下載,并自定義樣式,還提到了使用xlsx庫進(jìn)行Excel表格數(shù)據(jù)的導(dǎo)入,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-01-01基于Vue實(shí)現(xiàn)自定義組件的方式引入圖標(biāo)
在vue項(xiàng)目中我們經(jīng)常遇到圖標(biāo),下面這篇文章主要給大家介紹了關(guān)于如何基于Vue實(shí)現(xiàn)自定義組件的方式引入圖標(biāo)的相關(guān)資料,文章通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-07-07vue鼠標(biāo)hover(懸停)改變background-color移入變色問題
這篇文章主要介紹了vue鼠標(biāo)hover(懸停)改變background-color移入變色問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10