vue3 学习笔记17 -- 基于el-menu封装菜单

news2024/9/23 2:29:55

vue3 学习笔记17 – 基于el-menu封装菜单

前提条件:组件创建完成

配置路由

// src/router/index.ts
import { createRouter, createWebHashHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
export const Layout = () => import('@/layout/index.vue')
// 静态路由
export const routes: RouteRecordRaw[] = [
  {
    path: '/login',
    component: () => import('@/views/login/index.vue'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401.vue'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404.vue'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/home',
    children: [
      {
        path: 'home',

        component: () => import('@/views/home/index.vue'),
        name: 'Home',
        meta: { title: '首页', icon: 'home' }
      }
    ]
  },
  {
    path: '/permission',
    component: Layout,
    meta: {
      title: '权限管理',
      icon: 'permission'
    },
    children: [
      {
        path: 'user',

        component: () => import('@/views/permission/user/index.vue'),
        name: 'User',
        meta: { title: '用户管理'}
      },
      {
        path: 'role',

        component: () => import('@/views/permission/role/index.vue'),
        name: 'Role',
        meta: { title: '角色管理' }
      }
    ]
  }
]
const router = createRouter({
  history: createWebHashHistory(),
  routes: routes as RouteRecordRaw[],
  scrollBehavior: () => ({ left: 0, top: 0 })
})
/**
 * 重置路由
 */
export function resetRouter() {
  router.replace({ path: '/login' })
}

export default router

封装el-menu

  • 目录结构
    在这里插入图片描述

  • sidebar/index.vue

<script lang="ts" setup>
// sidebarItem 项组件
import SideBarItem from './sidebarItem.vue'
import { useRouter } from 'vue-router'
import { usePermissionStoreHook } from '@/stores/permission'
import { toRaw } from 'vue'
// 拿到路由列表,过滤我们不想要的
const router = useRouter()
const permissionStore = usePermissionStoreHook()
const routerList = toRaw(permissionStore.permission.routes)
console.log(routerList, '获取到的路由list')
const defaultOpenList = []
const activeMenu = ref()
function handleSelect(index, indexPath) {
  console.log(index, indexPath)
}
watch(
  () => router.currentRoute.value.path,
  (toPath) => {
    activeMenu.value = toPath
    //要执行的方法
    console.log(toPath)
  },
  { immediate: true, deep: true }
)
</script>
<template>
  <div class="sidebar">
    <!-- 导航菜单 -->
    <el-menu
      ref="elMenu"
      :default-active="activeMenu"
      @select="handleSelect"
      :default-openeds="defaultOpenList"
      router
    >
      <!-- 引入子组件 -->
      <SideBarItem :routerList="routerList" />
    </el-menu>

  </div>
</template>
<style lang="scss" scoped>
.sidebar {
  height: 100%;
  padding-top: 30px;

  :deep(.el-menu) {
    width: 100%;
    height: 100%;
    overflow-y: auto;
    background: transparent;
    border: none;
    .el-menu-item {
      font-size: 16px;
      height: 25px;
      padding: 0 30px;
      margin-bottom: 40px;
      background: transparent;
      display: flex;
      align-items: center;
      outline: none;
      box-sizing: border-box;
      &:last-child {
        margin-bottom: 0;
      }
      img {
        width: 24px;
        height: 24px;
        margin-right: 10px;
      }
      span {
        color: var(--vt-main-color);
      }
      &.is-active {
        position: relative;
        span {
          color: var(--vt-main-color);
          font-weight: 600;
          font-family: 'PingFangSC-Semibold';
        }
        &::after {
          content: '';
          position: absolute;
          width: 4px;
          height: 30px;
          background: #2b5ae8;
          left: 0;
          top: 0;
          bottom: 0;
          margin: auto;
          z-index: 999;
        }
      }
    }
    .el-sub-menu {
      font-size: 14px;
      margin-bottom: 40px;
      .el-menu-item {
        min-width: 179px;
        padding: 0 64px !important;
        outline: none;
        overflow: hidden;
        font-size: 14px;
        margin-bottom: 20px;
        &:last-child {
          margin-bottom: 0;
        }
        &:first-child {
          margin-top: 20px;
        }
        span {
          color: #797979;
        }
        &.is-active {
          span {
            font-family: 'PingFangSC-Semibold';
            font-weight: 600;
            color: var(--vt-main-color);
          }
        }
      }
      &.is-opened {
        .el-submenu__title {
          > span {
            color: var(--vt-main-color);
          }
        }
        &.is-active {
          .el-submenu__title {
            font-weight: 600;
          }
        }
      }
      .el-sub-menu__title {
        font-size: 16px;
        height: 25px;
        display: flex;
        align-items: center;
        padding: 0 30px !important;
        background: transparent;
        img {
          width: 24px;
          height: 24px;
          margin-right: 10px;
        }
        &:hover {
          background: transparent;
        }
        + .el-menu {
          .el-submenu__title {
            background: transparent;
            padding: 0 64px !important;
          }
          .el-submenu {
            .el-menu {
              .el-menu-item {
                padding: 0 100px !important;
                &.is-active {
                  padding: 0 100px !important;
                }
              }
            }
          }
        }
        // span {
        //   position: relative;
        //   &::after {
        //     content: '';
        //     @include imageURL('closeMenu.png');
        //     width: vw(5 * 2);
        //     height: vw(5 * 2);
        //     position: absolute;
        //     right: vw(-15 * 2);
        //     top: 0;
        //     bottom: 0;
        //     margin: auto;
        //   }
        // }
      }
      .el-sub-menu__icon-arrow {
        display: none;
      }
      &.is-opened {
        > .el-sub-menu__title {
          &:hover {
            background: transparent;
          }
        }
        .el-menu-item {
          padding: 0 64px !important;
          &.is-active {
            padding-left: 64px !important;
            font-weight: 600;
            color: var(--vt-main-color);
          }
        }
      }
      .el-submenu {
        margin-bottom: 20px;
        &:first-child {
          margin-top: 20px;
        }
      }
    }
  }
}
</style>

  • sidebar/siderbarItem.vue
<script lang="ts" setup>
defineProps({
  routerList: {
    type: Array,
    default: () => {
      return []
    }
  }
})
const getImageUrl = (iconName) => {
  return new URL(`../../../assets/menu/${iconName}.png`, import.meta.url).href
}
</script>
<template>
  <template v-for="menu in routerList">
    <el-sub-menu
      :key="menu.url"
      :index="menu.url"
      v-if="menu.children && menu.children.length > 0 && !menu.hidden"
    >
      <template #title>
        <img :src="getImageUrl(menu.meta.icon)" alt="" />
        <span class="menu-title">{{ menu.meta.title }}</span>
      </template>
      <sidebarItem :router-list="menu.children"></sidebarItem>
    </el-sub-menu>
    <el-menu-item :key="menu.meta.url + 'u'" :index="menu.meta.url" v-else-if="!menu.hidden">
      <img :src="getImageUrl(menu.meta.icon)" alt="" v-if="menu.meta.icon" />
      <span class="menu-title">{{ menu.meta.title }}</span>
    </el-menu-item>
  </template>
</template>

  • layout/index.vue – 引入菜单
<template>
  <div class="page-box">
    <div class="page-left">
      <div class="logo-box">
        <img src="@/assets/menu/logo-word.png" alt="" />
      </div>
      <sidebar class="side-menu"></sidebar>
    </div>
    <div class="page-right">
      <div class="header-box">
        <div class="title">让电看得见摸得着.</div>
      </div>
      <Layout></Layout>
    </div>
  </div>
</template>
<script setup lang="ts">
import Layout from './layout.vue'
import sidebar from './components/sidebar/index.vue'
</script>
<style lang="scss" scoped>
.page-box {
  width: 100%;
  height: 100%;
  display: flex;
  overflow: hidden;
  position: relative;
  background: #f0f0f0;
  .page-left {
    width: 258px;
    position: fixed;
    left: 6px;
    top: 0;
    bottom: 0;
    margin: auto;
    display: flex;
    flex-direction: column;
    background: #f0f0f0;
    .logo-box {
      height: 73px;
      display: flex;
      align-items: center;
      padding-left: 34px;
      width: 100%;
      img {
        width: 73px;
        height: 30px;
      }
    }
    .side-menu {
      width: 100%;
      flex: 1;
      background: url('@/assets/menu/bg.png') no-repeat;
      background-size: 100% 100%;
      overflow: hidden;
      transition: width 0.28s;
      margin-bottom: 60px;
      box-sizing: content-box;
    }
  }
  .page-right {
    flex: 1;
    height: 100%;
    transition: margin-left 0.28s;
    margin-left: 264px;
    display: flex;
    flex-direction: column;
    .header-box {
      position: fixed;
      width: 87.0625%;
      //210-230
      left: 260px;
      top: 0;
      padding: 0 50px;
      display: flex;
      align-items: center;
      height: 73px;
      justify-content: space-between;
      box-sizing: border-box;
      z-index: 9;
    }
  }
}
</style>

  • layout/lauout.vue
<template>
  <div id="app-main">
    <router-view v-slot="{ Component, route }">
      <transition name="router-fade" mode="out-in">
        <div key="route.fullPath">
          <component :is="Component" />
        </div>
      </transition>
    </router-view>
  </div>
</template>
<script setup lang="ts"></script>
<style lang="scss">
#app-main {
  flex: 1;
  height: 100%;
  padding-top: 73px;
  display: flex;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
  -ms-flex-direction: column;
  flex-direction: column;
  padding-left: 40px;
}
</style>

  • 运行项目查看页面

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1944495.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

FlutterFlame游戏实践#16 | 生命游戏 - 编辑与交互

theme: cyanosis 本文为稀土掘金技术社区首发签约文章&#xff0c;30天内禁止转载&#xff0c;30天后未获授权禁止转载&#xff0c;侵权必究&#xff01; Flutter\&Flame 游戏开发系列前言: 该系列是 [张风捷特烈] 的 Flame 游戏开发教程。Flutter 作为 全平台 的 原生级 渲…

ios 15-16手机绕过ssl验证(抓取app上的https包)

绕过ssl验证的基本流程 前提概要&#xff1a;为什么你的charles抓不了https包 ios 越狱ios rootful安装ios 越狱商店sileo安装substitute越狱商店安装SSL Kill Switch3 全流程坑点巨多&#xff0c;博主亲身踩坑&#xff0c;务必按着步骤来 准备工作 type b to c 的数据线苹果…

读论文《Hi-Net: Hybrid-fusion Network for Multi-modalMR Image Synthesis》

论文题目&#xff1a;Hi-Net:用于多模态磁共振图像合成的混合融合网络 论文地址&#xff1a;arxiv 项目地址&#xff1a;github 原项目可能在训练的时候汇报version的错&#xff0c;这是因为生成器和辨别器的优化有些逻辑错误&#xff0c;会改的话多加一个生成操作可以解决&…

数字信号处理基础知识(二)

在介绍完“离散时间序列”基本概念和性质后&#xff0c;实际上就已经踏入了“数字信号处理”这门学科的学习征程&#xff0c;这篇文章里主要去说明“线性时不变系统”的定义概念和探讨“周期采样”的注意细节&#xff0c;相信更加理解这些概念定义和底层逻辑&#xff0c;对于大…

python+vue3+onlyoffice在线文档系统实战20240723笔记,项目界面设计和初步开发

经过之前的学习,已经能够正常打开文档了。 目前为止,我们的代码能够实现: 打开文档编辑文档手动保存自动保存虽然功能依然比较少,但是我们已经基本实现了文档管理最核心的功能,而且我们有个非常大的优势,就是支持多人同时在线协同编辑。 现在我们要开发项目,我们得做基…

Golang | Leetcode Golang题解之第279题完全平方数

题目&#xff1a; 题解&#xff1a; // 判断是否为完全平方数 func isPerfectSquare(x int) bool {y : int(math.Sqrt(float64(x)))return y*y x }// 判断是否能表示为 4^k*(8m7) func checkAnswer4(x int) bool {for x%4 0 {x / 4}return x%8 7 }func numSquares(n int) i…

Python的注释怎么写

今天我们讲一下Python的注释怎么写&#xff0c;Python的注释的写法主要就是用""" &#xff08;注释&#xff09;"""和 #&#xff08;注释&#xff08;多半就是一行&#xff09;&#xff09; 来写 第一种&#xff1a; 使用""" &…

【linux】Shell脚本三剑客之sed命令的详细用法攻略

✨✨ 欢迎大家来到景天科技苑✨✨ 🎈🎈 养成好习惯,先赞后看哦~🎈🎈 🏆 作者简介:景天科技苑 🏆《头衔》:大厂架构师,华为云开发者社区专家博主,阿里云开发者社区专家博主,CSDN全栈领域优质创作者,掘金优秀博主,51CTO博客专家等。 🏆《博客》:Python全…

从零开始:神经网络(1)——什么是人工神经网络

声明&#xff1a;本文章是根据网上资料&#xff0c;加上自己整理和理解而成&#xff0c;仅为记录自己学习的点点滴滴。可能有错误&#xff0c;欢迎大家指正。 人工神经网络&#xff08;Artificial Neural Network&#xff0c;简称ANN&#xff09;是一种模仿生物神经网络结构和功…

【vue教程】三. 组件复用和通信(7 种方式)

目录 本章涵盖知识点回顾 组件开发与复用组件的创建和注册全局定义局部定义单文件组件&#xff08;.vue 文件&#xff09;组件的注册方式在实例中注册在 Vue 中注册 组件的 props定义 props传递 props 组件事件自定义事件的创建和触发父组件监听子组件事件父组件处理事件 Vue 实…

网格布局 HTML CSS grid layout demo

文章目录 页面效果代码 (HTML CSS)参考 页面效果 代码 (HTML CSS) <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"…

Golang | Leetcode Golang题解之第275题H指数II

题目&#xff1a; 题解&#xff1a; func hIndex(citations []int) int {n : len(citations)return n - sort.Search(n, func(x int) bool { return citations[x] > n-x }) }

你了解GD32 MCU上下电要求吗

你了解GD32 MCU的上下电要求吗&#xff1f;MCU的上下电对于系统的稳定运行非常重要。 以GD32F30X为例&#xff0c;上电/掉电复位波形如如下图所示。 上电过程中&#xff0c;VDD/VDDA电压上电爬坡&#xff0c;当电压高于VPOR&#xff08;上电复位电压&#xff09;MCU开始启动&a…

设计测试用例的具体方法

一.等价类 等价类分为: 1.有效等价类 [6~15] 2.无效等价类 :小于6位,大于15位(不在数据范围内) 组合规则: 有效等价类组合的时候,尽可能一条测试用例尽可能多的覆盖有效等价类 无效等价类组合的时候,一条测试点,之恶能覆盖一个无效等价类 二.边界值 1.上点,离点,内点 上…

科技引领水资源管理新篇章:深入剖析智慧水利解决方案,展现其在提升水资源利用效率、优化水环境管理方面的创新实践

本文关键词&#xff1a;智慧水利、智慧水利工程、智慧水利发展前景、智慧水利技术、智慧水利信息化系统、智慧水利解决方案、数字水利和智慧水利、数字水利工程、数字水利建设、数字水利概念、人水和协、智慧水库、智慧水库管理平台、智慧水库建设方案、智慧水库解决方案、智慧…

git clone超时的解决方法

问题描述&#xff1a;在克隆一个仓库的时候&#xff0c;报错如下 git clone https://github.com/TeamWiseFlow/wiseflow.git Cloning into wiseflow... fatal: unable to access https://github.com/TeamWiseFlow/wiseflow.git/: Failed to connect to github.com port 443 aft…

【PyTorch】图像二分类项目

【PyTorch】图像二分类项目 【PyTorch】图像二分类项目-部署 【PyTorch】图像多分类项目 【PyTorch】图像多分类项目部署 图像分类是计算机视觉中的一项重要任务。在此任务中&#xff0c;我们假设每张图像只包含一个主对象。在这里&#xff0c;我们的目标是对主要对象进行分类。…

C#开源、简单易用的Dapper扩展类库 - Dommel

项目特性 Dommel 使用 IDbConnection 接口上的扩展方法为 CRUD 操作提供了便捷的 API。 Dommel 能够根据你的 POCO 实体自动生成相应的 SQL 查询语句。这大大减少了手动编写 SQL 代码的工作量&#xff0c;并提高了代码的可读性和可维护性。 Dommel 支持 LINQ 表达式&#xff…

论文阅读——Integrated Diffusive Antenna Array of Low Backscattering

文章目录 摘要一、背景介绍二、天线结构A. 缝隙天线B. 低频扩散单元C. 高频扩散单元D. 集成设计 三、验证总结 论文来源&#xff1a;https://ieeexplore.ieee.org/document/10309141 摘要 文章提出了一种低雷达散射截面&#xff08;RCS&#xff09;的扩散天线阵列。 作为示例…

axios请求大全

本文讲解axios封装方式以及针对各种后台接口的请求方式 axios的介绍和基础配置可以看这个文档: 起步 | Axios中文文档 | Axios中文网 axios的封装 axios封装的重点有三个&#xff0c;一是设置全局config,比如请求的基础路径&#xff0c;超时时间等&#xff0c;第二点是在每次…