element自定义菜单导航

本文介绍了一个基于Vue框架的菜单配置与实现方法,通过定义菜单结构和使用Vue组件来展示菜单项。文章展示了如何组织菜单数据结构,并利用Vue的特性实现响应式的菜单交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. router/menu.js

"use strict";
export default {

  notExists() {
    var notExist = [
      {
        pkId: 1,
        orderNum: 1,
        path: "'/404",
        title: "无效账户",
        ukName: "无效账户",
        icon: ""

      }
    ];
    return notExist;
  },
  meuns() {
    var arrs = [
      {
        pkId: "1",
        orderNum: "1",
        path: "/home",
        title: "政策管理",
        ukName: "政策管理",
        icon: "user",
        children: [
          {
            pkId: "11",
            orderNum: "11",
            path: "/backgroundPage",
            title: "政策管理",
            ukName: "政策管理",
            icon: "xin"
          },
        ]
      },
      {
        pkId: "2",
        orderNum: "2",
        title: "用户记录",
        ukName: "用户记录",
        icon: "xin",
        children: [
          {
            pkId: "21",
            orderNum: "21",
            path: "/home",
            title: "访问记录",
            ukName: "访问记录",
            icon: "xin"
          }
        ]
      },
    ];
    return arrs;
  }
};

2.layout\components\Sidebar\index.vue 

<el-scrollbar wrap-class="scrollbar-wrapper">
  <el-menu
    :default-active="activeMenu"
    :collapse="isCollapse"
    :background-color="variables.menuBg"
    :text-color="variables.menuText"
    :unique-opened="false"
    :active-text-color="variables.menuActiveText"
    :collapse-transition="false"
    mode="vertical"
    router>
    <!--<sidebar-item v-for="route in routes" :key="route.pkId" :item="route" :base-path="route.path" />-->
    <!--方式二:加载路由-->
    <sidebar-item :navMenus="routes"></sidebar-item><!--方式一:自定菜单-->
  </el-menu>
</el-scrollbar>

 import Meun from "@/router/meun.js";

routes() {
  return Meun.meuns();
},

 

 

3.layout\components\Sidebar\SidebarItem.vue  

 

<!--菜单子组件-->
<template>
  <div class="navMenu">
    <label v-for="(navMenu,index) in navMenus" :key="index">
      <el-menu
        class="el-menu-vertical-demo"
        background-color="#3e3a58"
        text-color="#fff"
        active-text-color="#ffd04b"
        @select="handleSelect"
        :default-active="activeIndex"
      >
        <el-submenu :index="navMenu.ukName" v-if="navMenu.children">
          <template slot="title">
            <svg-icon :icon-class="navMenu.icon"/>
            <span>{{navMenu.ukName}}</span>
          </template>
          <el-menu-item v-for="(child,index) in navMenu.children" :key="index" :index="child.pkId" @click="childPath(child.path)">
            {{child.ukName}}
          </el-menu-item>
        </el-submenu>
        <el-menu-item :index="navMenu.ukName" v-else @click="toPath(navMenu.path)">
          <svg-icon :icon-class="navMenu.icon"/>
          <span slot="title">{{navMenu.ukName}}</span>
        </el-menu-item>
      </el-menu>
    </label>
  </div>
</template>
<script>
  export default {
    name: 'sidebar-item',
    props: ['navMenus'],
    data() {
      return {
        current:0,
        activeIndex:'1',
      }
    },
    methods: {
      addClass:function(index){
        this.current=index;
      },
      handleSelect(key, keyPath) {
        this.activeIndex=key;
      },
      childPath(keyPath){
        this.$router.push({path: keyPath})
        console.log(keyPath)
      },
      toPath(keyPath){
        this.$router.push({path: keyPath})
        console.log(keyPath)
      }
    }
  }
</script>

<style lang="scss" scoped>
  .el-menu-item{
    color: #ffffff!important;
    font-size: 14px;
    letter-spacing: 2px;
    vertical-align: middle;
  }
  .el-menu-item, .el-submenu__title {
    height: 50px;
    line-height: 50px;
  }
  .svg-icon{
    color: #ffffff;
    font-size: 22px;
    width: 1.2em!important;
    vertical-align: middle!important;
  }
  .active {
    color: #9ea0e9 !important;
    border-right: 10px solid;
    svg{
      color: #9ea0e9;
    }
  }
  .is-active{
    color: #9ea0e9 !important;
    background-color: #322e46 !important;
  }
</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值