vue+elementUI实现动态面包屑

作者:写Bug的大雄 时间:2024-05-02 17:11:02 

本文实例为大家分享了vue+elementUI实现动态面包屑的具体代码,供大家参考,具体内容如下

引言

后台管理系统中,经常会出现需要面包屑的情况,但是又不想每个页面都实现一个,这样不方便维护,因此封装了面包屑组件,方便在页面使用

封装组件

<!-- Breadcrumb/index.vue --> ? ?
<template>
? <div>
? ? <el-breadcrumb class="breadcrumb" separator="/">
? ? ? <transition-group name="breadcrumb">
? ? ? ? <el-breadcrumb-item v-for="(item, index) in breadList" :key="item.path">
? ? ? ? ? <span
? ? ? ? ? ? v-if="
? ? ? ? ? ? ? item.redirect === $route.path || index == breadList.length - 1
? ? ? ? ? ? "
? ? ? ? ? >
? ? ? ? ? ? {{ item.meta.title }}
? ? ? ? ? </span>
? ? ? ? ? <a v-else @click.prevent="handleLink(item)">{{ item.meta.title }}</a>
? ? ? ? </el-breadcrumb-item>
? ? ? </transition-group>
? ? </el-breadcrumb>
? </div>
</template>

<script lang="ts">
import Vue from 'vue';

export default Vue.extend({
? data () {
? ? return {
? ? ? // 路由集合
? ? ? breadList: [] as any[]
? ? };
? },
? methods: {
? ? // 判断是否包含首页路由
? ? isDashboard (route: { name: string }) {
? ? ? const name = route && route.name;
? ? ? if (!name) {
? ? ? ? return false;
? ? ? }
? ? ? return route.name === 'Dashboard';
? ? },
? ? // 面包屑跳转
? ? handleLink (item: { redirect: any; path: any }) {
? ? ? const { redirect, path } = item;
? ? ? redirect ? this.$router.push(redirect) : this.$router.push(path);
? ? },
? ? // 判断当前面包屑
? ? init () {
? ? ? this.breadList = [];
? ? ? this.$route.matched.forEach((item) => {
? ? ? ? if (item.meta.title) {
? ? ? ? ? this.breadList.push(item);
? ? ? ? }
? ? ? });

? ? ? if (!this.isDashboard(this.breadList[0])) {
? ? ? ? this.breadList.unshift({
? ? ? ? ? path: '/dashboard/index',
? ? ? ? ? meta: { title: '首页' }
? ? ? ? });
? ? ? }
? ? }
? },
? created () {
? ? this.init();
? },
? // 当组件放在总布局组件中,需要监听路由变化
? watch: {
? ? $route () {
? ? ? this.init();
? ? }
? }
});
</script>

<style lang="less" scoped>
.breadcrumb-enter-active,
.breadcrumb-leave-active {
? transition: all 0.5s;
}

.breadcrumb-enter,
.breadcrumb-leave-active {
? opacity: 0;
? transform: translateX(20px);
}

.breadcrumb-move {
? transition: all 0.5s;
}

.breadcrumb-leave-active {
? position: absolute;
}
</style>

页面使用

<template>
? <div>
? ? <my-breadcrumb></my-breadcrumb>
? ? four
? </div>
</template>

<script lang="ts">
import Vue from 'vue';
import MyBreadcrumb from '@/components/Breadcrumb/index.vue';

export default Vue.extend({
? components: {
? ? MyBreadcrumb
? }
});
</script>

<style scoped>
</style>

路由文件参考

// router/index.ts

import Vue from 'vue';
import VueRouter from 'vue-router';
import Login from '@/views/login/index.vue';
import Layout from '@/layout/index.vue';

Vue.use(VueRouter);

/**
?* hidden 表示是否需要在侧边导航栏出现 ,true表示不需要
?* isFirst 表示是否只有一级权限,只出现在只有一个子集,没有其他孙子集
?* 当权限拥有多个子集或者孙子集,一级权限需要加上 meta
?* 二级权限拥有子集,也必须有 meta
?*/

// 基础路由
export const constantRoutes = [
? {
? ? path: '/redirect',
? ? component: Layout,
? ? hidden: true,
? ? children: [
? ? ? {
? ? ? ? path: '/redirect/:path(.*)',
? ? ? ? component: () => import('@/views/redirect/index.vue')
? ? ? }
? ? ]
? },
? {
? ? path: '/',
? ? redirect: '/dashboard',
? ? hidden: true
? },
? {
? ? path: '/login',
? ? name: 'Login',
? ? component: Login,
? ? hidden: true
? },
? {
? ? path: '/dashboard',
? ? component: Layout,
? ? redirect: '/dashboard/index',
? ? isFirst: true,
? ? children: [
? ? ? {
? ? ? ? path: 'index',
? ? ? ? name: 'Dashboard',
? ? ? ? component: () => import('@/views/dashboard/index.vue'),
? ? ? ? meta: {
? ? ? ? ? title: '首页',
? ? ? ? ? icon: 'el-icon-location'
? ? ? ? }
? ? ? }
? ? ]
? }
];

// 动态路由
export const asyncRoutes = [
? {
? ? path: '/form',
? ? component: Layout,
? ? redirect: '/form/index',
? ? isFirst: true,
? ? children: [
? ? ? {
? ? ? ? path: 'index',
? ? ? ? name: 'Form',
? ? ? ? component: () => import('@/views/form/index.vue'),
? ? ? ? meta: {
? ? ? ? ? title: '表单',
? ? ? ? ? role: 'form',
? ? ? ? ? icon: 'el-icon-location'
? ? ? ? }
? ? ? }
? ? ]
? },
? {
? ? path: '/editor',
? ? component: Layout,
? ? redirect: '/editor/index',
? ? meta: {
? ? ? role: 'editors',
? ? ? title: '总富文本',
? ? ? icon: 'el-icon-location'
? ? },
? ? children: [
? ? ? {
? ? ? ? path: 'index',
? ? ? ? name: 'Editor',
? ? ? ? component: () => import('@/views/editor/index.vue'),
? ? ? ? meta: {
? ? ? ? ? title: '富文本',
? ? ? ? ? role: 'editor',
? ? ? ? ? icon: 'el-icon-location'
? ? ? ? }
? ? ? },
? ? ? {
? ? ? ? path: 'two',
? ? ? ? name: 'Two',
? ? ? ? redirect: '/editor/two/three',
? ? ? ? component: () => import('@/views/editor/two.vue'),
? ? ? ? meta: {
? ? ? ? ? title: '二级导航',
? ? ? ? ? role: 'two',
? ? ? ? ? icon: 'el-icon-location'
? ? ? ? },
? ? ? ? children: [
? ? ? ? ? {
? ? ? ? ? ? path: 'three',
? ? ? ? ? ? name: 'Three',
? ? ? ? ? ? component: () => import('@/views/editor/three.vue'),
? ? ? ? ? ? meta: {
? ? ? ? ? ? ? title: ' * 导航',
? ? ? ? ? ? ? role: 'three',
? ? ? ? ? ? ? icon: 'el-icon-location'
? ? ? ? ? ? }
? ? ? ? ? },
? ? ? ? ? {
? ? ? ? ? ? path: 'four',
? ? ? ? ? ? name: 'Four',
? ? ? ? ? ? component: () => import('@/views/editor/four.vue'),
? ? ? ? ? ? meta: {
? ? ? ? ? ? ? title: ' * 导航2',
? ? ? ? ? ? ? role: 'four',
? ? ? ? ? ? ? icon: 'el-icon-location'
? ? ? ? ? ? }
? ? ? ? ? }
? ? ? ? ]
? ? ? }
? ? ]
? },
? {
? ? path: '/tree',
? ? component: Layout,
? ? redirect: '/tree/index',
? ? isFirst: true,
? ? children: [
? ? ? {
? ? ? ? path: 'index',
? ? ? ? name: 'Tree',
? ? ? ? component: () => import('@/views/tree/index.vue'),
? ? ? ? meta: {
? ? ? ? ? title: '树状图',
? ? ? ? ? role: 'tree',
? ? ? ? ? icon: 'el-icon-location'
? ? ? ? }
? ? ? }
? ? ]
? },
? {
? ? path: '/excel',
? ? component: Layout,
? ? redirect: '/excel/index',
? ? isFirst: true,
? ? children: [
? ? ? {
? ? ? ? path: 'index',
? ? ? ? name: 'Excel',
? ? ? ? component: () => import('@/views/excel/index.vue'),
? ? ? ? meta: {
? ? ? ? ? title: '导入导出',
? ? ? ? ? role: 'excel',
? ? ? ? ? icon: 'el-icon-location'
? ? ? ? }
? ? ? }
? ? ]
? }
];

// 出错跳转的路由
export const error = [
? // 404
? {
? ? path: '/404',
? ? component: () => import('@/views/error/index.vue'),
? ? hidden: true
? },
? {
? ? path: '*',
? ? redirect: '/404',
? ? hidden: true
? }
];

const createRouter = () =>
? new VueRouter({
? ? scrollBehavior: () => ({
? ? ? x: 0,
? ? ? y: 0
? ? }),
? ? routes: constantRoutes
? });

const router = createRouter();

// 刷新路由
export function resetRouter () {
? const newRouter = createRouter();
? (router as any).matcher = (newRouter as any).matcher;
}

export default router;

参考网上资料进行封装修改,具体需求可根据项目修改

来源:https://blog.csdn.net/m0_64344940/article/details/122756814

标签:vue,elementUI,面包屑
0
投稿

猜你喜欢

  • 详解Javascript 装载和执行

    2024-04-18 09:41:02
  • Mysql的列修改成行并显示数据的简单实现

    2024-01-24 01:39:54
  • Python实现PIL图像处理库绘制国际象棋棋盘

    2021-06-20 14:18:08
  • Go语言题解LeetCode463岛屿的周长示例详解

    2024-04-25 15:09:59
  • Python 使用 attrs 和 cattrs 实现面向对象编程的实践

    2022-05-29 22:38:00
  • 用一句SQL解决SQL中断号问题 推荐

    2024-01-13 22:10:56
  • python网络爬虫学习笔记(1)

    2023-12-24 04:08:16
  • sql 查询记录数结果集某个区间内记录

    2023-07-09 08:25:01
  • 如何使用python传入不确定个数参数

    2023-10-27 22:56:36
  • JavaScript正则表达式验证中文实例讲解

    2024-04-10 10:55:59
  • Python正则表达式匹配ip地址实例

    2023-06-13 15:06:12
  • js的Prototype属性解释及常用方法

    2024-06-07 16:00:32
  • Linux安装Python3.8.1的教程详解

    2022-03-26 10:43:24
  • 简单聊一聊SQL中的union和union all

    2024-01-18 00:17:17
  • 在ASP.NET 2.0中操作数据之五十五:编辑和删除现有的二进制数据

    2023-07-10 02:05:43
  • Python开发之基于模板匹配的信用卡数字识别功能

    2021-08-09 22:30:27
  • python爬虫设置每个代理ip的简单方法

    2022-11-01 19:18:52
  • vue项目打包之后接口出现错误的问题及解决

    2024-05-09 15:11:25
  • CI框架出现mysql数据库连接资源无法释放的解决方法

    2023-11-15 07:13:35
  • pycharm第三方库安装失败的问题及解决经验分享

    2023-07-11 01:50:45
  • asp之家 网络编程 m.aspxhome.com