element-plus实现面包屑功能

<template>
    <div class="f-tag-list" :style="{ left:$store.state.asideWidth }">
  
        <el-tabs v-model="activeTab" type="card" class="flex-1" @tab-remove="removeTab" style="min-width:100px;"
        @tab-change="changeTab">
            <el-tab-pane :closable="item.path != '/'" v-for="item in tabList" :key="item.path" :label="item.title" :name="item.path"></el-tab-pane>
        </el-tabs>
  
        <span class="tag-btn">
            <el-dropdown @command="handleClose">
                <span class="el-dropdown-link">
                    <el-icon>
                        <arrow-down />
                    </el-icon>
                </span>
                <template #dropdown>
                    <el-dropdown-menu>
                        <el-dropdown-item command="clearOther">关闭其他</el-dropdown-item>
                        <el-dropdown-item command="clearAll">全部关闭</el-dropdown-item>
                    </el-dropdown-menu>
                </template>
            </el-dropdown>
        </span>
    </div>
    <div style="height:44px;"></div>
  </template>
  <script setup>
  import { useTabList } from "@/utils/useTabList"
  const {
    activeTab,
    tabList,
    changeTab,
    removeTab,
    handleClose
  } = useTabList()
  </script>
  <style scoped>
  .f-tag-list{
    @apply fixed bg-gray-100 flex items-center px-2;
    top: 64px;
    right: 0;
    height: 44px;
    z-index: 100;
  }
  .tag-btn{
    @apply bg-white rounded ml-auto flex items-center justify-center px-2;
    height: 32px;
  }
  :deep(.el-tabs__header){
    border: 0!important;
    @apply mb-0;
  }
  :deep(.el-tabs__nav){
    border: 0!important;
  }
  :deep(.el-tabs__item){
    border: 0!important;
    height: 32px;
    line-height: 32px;
    @apply bg-white mx-1 rounded;
  }
  :deep(.el-tabs__nav-next),:deep(.el-tabs__nav-prev){
    line-height: 32px;
    height: 32px;
  }
  :deep(.is-disabled){
    cursor: not-allowed;
    @apply text-gray-300;
  }
  </style>

useTabList.js文件

import { ref } from 'vue'
import { useRoute, onBeforeRouteUpdate } from 'vue-router';
import { useCookies } from '@vueuse/integrations/useCookies'
import { router } from '@/router';

export function useTabList() {
    const route = useRoute()
    const cookie = useCookies()

    const activeTab = ref(route.path)
    const tabList = ref([
        {
            title: '后台首页',
            path: "/"
        },
    ])

    //监听侧边菜单路由变化
    onBeforeRouteUpdate((to, from) => {
        //设置当前处于激活状态
        activeTab.value = to.path
        addTab({
            title: to.meta.title,
            path: to.path
        })
    })
    // 添加标签导航
    function addTab(tab) {
        let noTab = tabList.value.findIndex(t => t.path == tab.path) == -1
        if (noTab) {
            tabList.value.push(tab)
        }

        cookie.set("tabList", tabList.value)
    }

    // 初始化标签导航列表
    function initTabList() {
        let tbs = cookie.get("tabList")
        if (tbs) {
            tabList.value = tbs
        }
    }

    initTabList()
    //点击标签到对应界面
    const changeTab = (t) => {
        // console.log(route.path);
        activeTab.value = t
        router.push(t)
    }
    //移除标签功能
    const removeTab = (t) => {
        let tabs = tabList.value
        let a = activeTab.value
        if (a == t) {
            tabs.forEach((tab, index) => {
                if (tab.path == t) {
                    const nextTab = tabs[index + 1] || tabs[index - 1]
                    if (nextTab) {
                        a = nextTab.path
                    }
                }
            })
        }

        activeTab.value = a
        tabList.value = tabList.value.filter(tab => tab.path != t)

        cookie.set("tabList", tabList.value)
    }
    //关闭其他/全部关闭
    const handleClose = (c) => {
        if (c == "clearAll") {
            // 切换回首页
            activeTab.value = "/"
            // 过滤只剩下首页
            tabList.value = [{
                title: '后台首页',
                path: "/"
            }]
        } else if (c == "clearOther") {
            // 过滤只剩下首页和当前激活
            tabList.value = tabList.value.filter(tab => tab.path == "/" || tab.path == activeTab.value)
        }
        cookie.set("tabList", tabList.value)
    }

    return {
        activeTab,
        tabList,
        changeTab,
        removeTab,
        handleClose
    }
}

效果:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
element-plus 中,可以使用 `el-breadcrumb` 组件来实现静态面包屑。如果需要实现动态面包屑,可以结合路由信息和监听路由变化的事件来实现。 具体步骤如下: 1. 在 `router/index.js` 中定义每个路由对应的面包屑信息,例如: ```javascript const routes = [ { path: '/', name: 'Home', component: Home, meta: { breadcrumb: '首页' } }, { path: '/about', name: 'About', component: () => import('@/views/About.vue'), meta: { breadcrumb: '关于我们' } }, // 其他路由定义 ] ``` 2. 在页面中使用 `el-breadcrumb` 组件,并通过 `v-for` 指令动态绑定面包屑信息: ```html <el-breadcrumb separator-class="el-icon-arrow-right"> <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item> <el-breadcrumb-item v-for="(item, index) in dynamicBreadcrumb" :key="index">{{ item.name }}</el-breadcrumb-item> </el-breadcrumb> ``` 其中,`dynamicBreadcrumb` 是一个数组,存储当前页面的动态面包屑信息。 3. 监听路由变化的事件,在路由变化时更新 `dynamicBreadcrumb` 数组: ```javascript export default { data() { return { dynamicBreadcrumb: [] } }, mounted() { this.$router.afterEach(route => { const matchedRoutes = route.matched const dynamicBreadcrumb = [] for (const route of matchedRoutes) { if (route.meta && route.meta.breadcrumb) { dynamicBreadcrumb.push({ name: route.meta.breadcrumb, path: route.path }) } } this.dynamicBreadcrumb = dynamicBreadcrumb }) } } ``` 在监听路由变化的 `afterEach` 钩子中,获取当前路由的 `matched` 数组,遍历每个路由,如果定义了 `meta.breadcrumb` 属性,则将该属性的值添加到 `dynamicBreadcrumb` 数组中。最后将 `dynamicBreadcrumb` 数组绑定到页面上,即可实现动态面包屑。 以上是 element-plus 实现动态面包屑的一种方法,您也可以根据实际需求进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值