最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于vue-router的matched實(shí)現(xiàn)面包屑功能

 更新時(shí)間:2021年09月26日 12:30:37   作者:月半小夜曲_  
本文主要介紹了基于vue-router的matched實(shí)現(xiàn)面包屑功能,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文主要介紹了基于vue-router的matched實(shí)現(xiàn)面包屑功能,分享給大家,具體如下:

如上圖所示,就是常見的面包屑效果,面包屑的內(nèi)容一般來說,具有一定的層級(jí)關(guān)系,就以上圖為例,首先是進(jìn)入首頁,然后點(diǎn)擊左側(cè)導(dǎo)航,進(jìn)入活動(dòng)管理下的活動(dòng)列表頁面,然后點(diǎn)擊某一條數(shù)據(jù),進(jìn)入活動(dòng)詳情頁面

這正好與vue-router的mached屬性所獲取的結(jié)果有著相似的原理,所以可以基于此來實(shí)現(xiàn)面包屑效果!

這里我使用了elementui的面包屑組件和導(dǎo)航菜單組件,先貼出最后的效果圖:

路由配置

項(xiàng)目結(jié)構(gòu):

側(cè)邊導(dǎo)航欄是首頁、電子數(shù)碼、服裝鞋帽頁面都會(huì)顯示的,所以我創(chuàng)建了一個(gè)layout組件,將這三個(gè)路由的component都指向該組件,并將導(dǎo)航欄和面包屑都寫在layout組件中

因?yàn)樵摴δ艿膶?shí)現(xiàn)依賴于路由的層級(jí)嵌套關(guān)系,所以要提前構(gòu)思好路由的配置,我這里的路由配置如下:

const routes = [
 //匹配空路由,重定向到根路由
    {
        path:'',
        redirect: '/home',
        meta:{
            showInbreadcrumb:false
        }
    },
    //根路由
    {
        path:'/home',
        component: ()=>import('@/views/layout/index.vue'),
        name:'home',
        meta:{
            title:"首頁",
            showInbreadcrumb:true
        }
    },
    //電子數(shù)碼
    {
        path:'/electronics',
        name:'電子數(shù)碼',
        component: ()=>import('@/views/layout/index.vue'),
        redirect: '/electronics/computer',
        meta:{
            title:"電子數(shù)碼",
            showInbreadcrumb:true
        },
        children:[
            {
                path:'computer',
                name:'computer',
                component()=>import('@/views/electronics/children/computer/index.vue'),
                meta:{
                    title:"電腦",
                    showInbreadcrumb:true
                }
            },
            {
                path:'phone',
                name:'手機(jī)',
                component: ()=>import('@/views/electronics/children/phone/index.vue'),
                meta:{
                    title:"手機(jī)",
                    showInbreadcrumb:true
                }
            },
            {
                path:'tv',
                name:'電視',
                component: ()=>import('@/views/electronics/children/tv/index.vue'),
                meta:{
                    title:"電視",
                    showInbreadcrumb:true
                }
            }
        ]
    },
    //服裝鞋帽
    {
        path:'/clothing',
        name:'服裝鞋帽',
        component: ()=>import('@/views/layout/index.vue'),
        redirect: '/clothing/tops',
        meta:{
            title:"服裝鞋帽",
            showInbreadcrumb:true
        },
        children:[
            {
                path:'tops',
                name:'上裝',
                component:  ()=>import('@/views/clothing/children/tops/index.vue'),
                meta:{
                    title:"上裝",
                    showInbreadcrumb:true
                }
            },
            {
                path:'lower',
                name:'下裝',
                component:  ()=>import('@/views/clothing/children/lower/index.vue'),
                meta:{
                    title:"下裝",
                    showInbreadcrumb:true
                }
            },
            {
                path:'shoes',
                name:'鞋子',
                component:  ()=>import('@/views/clothing/children/shoes/index.vue'),
                meta:{
                    title:"鞋子",
                    showInbreadcrumb:true
                }
            }
        ]
    },
    //放在最后,當(dāng)前面所有路由都沒匹配到時(shí),會(huì)匹配該路由,并重定向到根路由
    {
        path:'*',
        redirect:'/',
        meta:{
            showInbreadcrumb:false
        } 
    },   
]

這里我配置的路由有首頁、電子數(shù)碼、服裝鞋帽,這三個(gè)是一級(jí)路由,其中電子數(shù)碼和服裝鞋帽還有二級(jí)路由,在meta中我自定義了數(shù)據(jù),showInbreadcrumb用于判斷是否顯示在面包屑中,title為在面包屑顯示的名稱

獲取路由信息

模板部分:

///src/views/layout/index.vue
<template>
    <div class="layout">
        <!-- 側(cè)邊導(dǎo)航欄 -->
        <div class="sideMenu">
            <el-menu
                default-active="0"
                class="el-menu-vertical-demo"
                >
                    <div v-for="(item,index) in routes" :key="index" :index="index+''">
                        <!-- 沒有二級(jí)菜單的 -->
                        <el-menu-item :index="index+''" v-if="!item.children">
                            <router-link :to="{name:item.name}">{{item.meta.title}}</router-link>
                        </el-menu-item>
                        <!-- 有二級(jí)菜單的 -->
                        <el-submenu :index="index+''" v-else>
                            <template slot="title">{{item.meta.title}}</template>
                            <el-menu-item  v-for="(item_,index_) in item.children" :key="index_" :index="index+'-'+index_">
                                <router-link :to="{name:item_.name}">{{item_.meta.title}}</router-link>
                            </el-menu-item>
                        </el-submenu>
                    </div>
            </el-menu>
        </div>
        <div class="content">
            <!-- 面包屑 -->
            <div class="breadcrumb">
                <el-breadcrumb separator-class="el-icon-arrow-right">
                    <el-breadcrumb-item v-for="(item,index) in breadcrumb" :key="index" :to="{ path: item.path}">{{item.meta.title}}</el-breadcrumb-item>
                </el-breadcrumb>
            </div>
            <!-- 路由出口 -->
            <router-view></router-view>
        </div>
    </div>
</template>

js部分:

export default {
    data(){
        return{
        }
    },
    computed:{
        // 側(cè)邊導(dǎo)航數(shù)據(jù)
        routes(){
            // 從$router.options中獲取所有路由信息并過濾
            return this.$router.options.routes.filter((item)=>{
                return item.meta.showInbreadcrumb
            });
        },
        // 面包屑數(shù)據(jù)
        breadcrumb(){
            // 根據(jù)路由配置meta中的showInbreadcrumb字段過濾
            let matchedArr = this.$route.matched.filter((item)=>{
                return item.meta.showInbreadcrumb}
            );
            // 因?yàn)槭醉摫容^特殊,必須一直顯示在面包屑第一個(gè),如果沒有首頁路由信息,手動(dòng)添加到最前面
            if(matchedArr[0].meta.title !== '首頁'){
                matchedArr.unshift(
                    {
                        path:'/home',
                        meta:{  
                            title:"首頁",
                            showInbreadcrumb:true
                        }
                    },
                )
            }
            return matchedArr;
        },
    }
}

注意:拿到this.$route.matched后,不能在其結(jié)果上直接追加然后再過濾,否則會(huì)頁面錯(cuò)亂并且報(bào)錯(cuò),應(yīng)該先filter,這樣會(huì)返回一個(gè)新的數(shù)組,然后再判斷追加首頁信息

最終效果

到此這篇關(guān)于基于vue-router的matched實(shí)現(xiàn)面包屑功能的文章就介紹到這了,更多相關(guān)vue-router matched面包屑內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 通過一個(gè)簡單的例子學(xué)會(huì)vuex與模塊化

    通過一個(gè)簡單的例子學(xué)會(huì)vuex與模塊化

    這篇文章主要給大家介紹了關(guān)于如何通過一個(gè)簡單的例子學(xué)會(huì)vuex與模塊化的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-11-11
  • vue3中的reactive函數(shù)聲明數(shù)組方式

    vue3中的reactive函數(shù)聲明數(shù)組方式

    這篇文章主要介紹了vue3中的reactive函數(shù)聲明數(shù)組方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • vue中對(duì)token有效期的深入理解

    vue中對(duì)token有效期的深入理解

    本文主要介紹了vue中對(duì)token有效期的深入理解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • Vue.js 加入高德地圖的實(shí)現(xiàn)代碼

    Vue.js 加入高德地圖的實(shí)現(xiàn)代碼

    這篇文章主要介紹了Vue.js 加入高德地圖的實(shí)現(xiàn)方法,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-12-12
  • vue實(shí)現(xiàn)點(diǎn)擊某個(gè)div顯示與隱藏內(nèi)容功能實(shí)例

    vue實(shí)現(xiàn)點(diǎn)擊某個(gè)div顯示與隱藏內(nèi)容功能實(shí)例

    最近做項(xiàng)目有用到某個(gè)div顯示與隱藏內(nèi)容,所以下面這篇文章主要給大家介紹了關(guān)于vue實(shí)現(xiàn)點(diǎn)擊某個(gè)div顯示與隱藏內(nèi)容功能的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-12-12
  • Avue和Element-UI動(dòng)態(tài)三級(jí)表頭的實(shí)現(xiàn)

    Avue和Element-UI動(dòng)態(tài)三級(jí)表頭的實(shí)現(xiàn)

    本文主要介紹了Avue和Element-UI動(dòng)態(tài)三級(jí)表頭的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • vue3+Echarts頁面加載不渲染顯示空白頁面的解決

    vue3+Echarts頁面加載不渲染顯示空白頁面的解決

    這篇文章主要介紹了vue3+Echarts頁面加載不渲染顯示空白頁面的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • Vue3?Reactive響應(yīng)式原理邏輯詳解

    Vue3?Reactive響應(yīng)式原理邏輯詳解

    這篇文章主要介紹了Vue3?Reactive響應(yīng)式原理邏輯詳解,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-07-07
  • el-table表頭使用el-dropdown出現(xiàn)兩個(gè)下拉框的問題及解決方法

    el-table表頭使用el-dropdown出現(xiàn)兩個(gè)下拉框的問題及解決方法

    本文給大家分享el-table在固定右邊列時(shí),表頭使用el-dropdown會(huì)出現(xiàn)兩個(gè)下拉框的解決方法,感興趣的朋友跟隨小編一起看看吧
    2024-07-07
  • 如何使用elementUI組件實(shí)現(xiàn)表格的分頁及搜索功能

    如何使用elementUI組件實(shí)現(xiàn)表格的分頁及搜索功能

    最近在使用element-ui的表格組件時(shí),遇到了搜索框功能的實(shí)現(xiàn)問題,這篇文章主要給大家介紹了關(guān)于如何使用elementUI組件實(shí)現(xiàn)表格的分頁及搜索功能的相關(guān)資料,需要的朋友可以參考下
    2023-03-03

最新評(píng)論

滦平县| 沙坪坝区| 海淀区| 太保市| 上杭县| 山丹县| 灵石县| 双牌县| 亚东县| 开鲁县| 新邵县| 长乐市| 双峰县| 潜江市| 常熟市| 通州区| 西峡县| 青河县| 聂拉木县| 兴业县| 龙岩市| 齐齐哈尔市| 德格县| 海伦市| 溆浦县| 赤水市| 无极县| 三门县| 册亨县| 沧源| 长治县| 额尔古纳市| 三门县| 安丘市| 垫江县| 遵化市| 旬邑县| 铁岭县| 寻乌县| 武汉市| 公主岭市|