vue3实现面包屑-基础实现

1.在默认路由router文件下新增两个额外的文件page:存放额外的路由列表,注意这里需要引入有router-view视图的页面,这里我是采用let Main = () => import("@v/layout/Main.vue");代码:

在page中使用:

2.dynamicAddRouter处理不匹配的路由和注册page路由表到router中

3.面包屑组件:

3.状态管理代码:

import router from '@/router'

import { ClassTag } from '#/index'

import { defineStore } from 'pinia'

import { inject } from 'vue'

import type { RouteLocationNormalizedLoaded } from 'vue-router'

export const useTagStores = defineStore('Tag', {

    state: () => ({

        tagList: [new ClassTag("/accountmanage/accountlist", "商户列表", "home")],//路由列表

        reload :inject("reload") as any

    }),

    actions: {

        // 添加

        pushTag(route: RouteLocationNormalizedLoaded, index: number = -1) {

            const { fullPath, meta, name } = route

            // 如果当前路由是redirect,则返回不添加标签页

            if ("redirect" === name) {

                return;

            }

            let openTags = this.tagList

            let findIndex = openTags.find((item: ClassTag) => item.fullPath == fullPath)

            if (!findIndex) {

                var i = index > -1 ? index : openTags.length

                openTags.splice(i, 0, new ClassTag(fullPath, meta.title as string, name as string))

            }

        },

        // 妙处,清空列表在添加避免将其他模块的路由增加进去

        settagList(route: RouteLocationNormalizedLoaded) {

            const { fullPath, meta, name } = route

            let openTags = this.tagList

            openTags.splice(0)

            openTags.splice(1, 1, new ClassTag(fullPath, meta.title as string, name as string))

        },

        // 点击跳转

        headerTab(index: number) {

            let tag = this.tagList[index]

            let route = router.currentRoute.value

            if (tag.fullPath != route.fullPath) {

                router.push(tag.fullPath);

            }

        },

        /**

         * 右键菜单功能

         */

        Command({ code, fullPath }: { code: number, fullPath: string }) {

            let openTags = this.tagList

            let route = router.currentRoute.value

            //点击标签页索引

            let index = openTags.findIndex((item) => item.fullPath == fullPath);

            //当前激活状态的标签页索引

            let activeIndex = openTags.findIndex((item) => item.fullPath === route.fullPath);

            switch (code) {

                case 1:

                    this.reload();

                    break;

                case 2://关闭左侧所有标签页

                    openTags.splice(0, index);

                    if (activeIndex != index||activeIndex <= index) {

                        router.push(openTags[0]?.fullPath);

                    }

                    break;

                case 3://关闭右侧所有标签页

                    // 删除右侧所有标签页

                    openTags.splice(index + 1);

                    // 如果当前页面被删除,显示右击菜单页

                    if (activeIndex > index) {

                        router.push(openTags[index].fullPath);

                    }

                    break;

                case 4:

                    this.removePath(fullPath)

                    break;

                case 5: //关闭所有标签页

                    openTags.splice(1);

                    router.push(openTags[0].fullPath);

                    break;

            }

        },

        // 删除

        removePath(fullPath: string) {

            let openTags = this.tagList

            let index = openTags.findIndex(item => item.fullPath === fullPath)

            if (-1 != index) {

                this.removeTab(index)

            }

        },

        removeTab(index: number) {

            let openTags = this.tagList

            let tag = openTags[index]

            // 获取当前路由信息

            let route = router.currentRoute.value

            // 如果当前路由的 fullPath 等于 tag 的 fullPath,则说明需要进行路由跳转

            if (route.fullPath == tag.fullPath) {

                // openTags[index + 1] || openTags[index - 1] 判断跳转到哪个路由(下一个或上一个),并将该路由的全路径赋值给 fullPath 变量

                let { fullPath } = openTags[index + 1] || openTags[index - 1];

                router.push(fullPath);

            }

            // 在 openTags 中删除指定下标的元素

            openTags.splice(index, 1)

        },

    },

    persist: true//开启持久化

})

要在Vue3中实现Element Plus面包屑,可以按照以下步骤进行操作: 1. 首先,在main.js(或者其他入口文件)中导入Element Plus库并注册组件: ```javascript import { createApp } from 'vue' import ElementPlus from 'element-plus' import 'element-plus/lib/theme-chalk/index.css' const app = createApp(...) app.use(ElementPlus) ... ``` 2. 在需要使用面包屑的页面组件中,添加面包屑组件的代码,并根据路由的变化动态改变面包屑显示。可以使用`watch`来监听路由的变化。 ```javascript <template> <div> <el-breadcrumb separator-class="el-icon-arrow-right"> <el-breadcrumb-item v-for="(item, index) in breadList" :key="index"> {{ item.name }} </el-breadcrumb-item> </el-breadcrumb> ... </div> </template> <script> import { watch } from 'vue' import { useRoute } from 'vue-router' export default { setup() { const route = useRoute() const breadList = ref([]) watch( () => route.matched, () => { breadList.value = route.matched.map((route) => ({ name: route.meta.title, // 其他属性,比如路由链接等 })) } ) return { breadList } } } </script> ``` 3. 在路由配置中,为每个路由添加`meta`字段,用于存储面包屑的标题。例如: ```javascript const routes = [ { path: '/', name: 'Home', component: Home, meta: { title: '首页' } }, { path: '/about', name: 'About', component: About, meta: { title: '关于我们' } }, // 其他路由配置 ] ``` 通过以上步骤,你就可以在Vue3中实现Element Plus的面包屑功能了。当路由发生变化时,面包屑会根据当前路由动态改变显示内容。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [【Vue3】element ui plus面包屑vue-route结合使用](https://blog.csdn.net/woodwhale/article/details/123944372)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值