惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
小众软件
小众软件
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - Franky
Jina AI
Jina AI
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Y
Y Combinator Blog
V
Visual Studio Blog
C
Check Point Blog
阮一峰的网络日志
阮一峰的网络日志
U
Unit 42
量子位
人人都是产品经理
人人都是产品经理
博客园 - 聂微东
M
MIT News - Artificial intelligence
爱范儿
爱范儿
B
Blog RSS Feed
MyScale Blog
MyScale Blog
H
Help Net Security
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
美团技术团队
L
LangChain Blog
D
Docker

博客园 - 一丝心情

Volta 安装和使用指南 免费国产ai试用总结 glb格式3d模型压缩 nuxt.js 项目流水线自动部署设置 免费的云数据库 vue 实用指令 IntelliJ IDEA license server 激活(亲测有效) win10/win11专业版激活码(亲测有效) Git 常用指令完全指南 npm 安装依赖报错整理 css3文字渐变, svg文字渐变 http/https与websocket的ws/wss的关系 指令 v-tooltip vue 自定义指令实现v-overflow-tooltip 常用css vue.config.js config.resolve.alias 目录别名配置 three.js 在低版本浏览报THREE.WebGLProgran: shader error 报错解决办法 nuxt 低版本浏览器兼容babel编译配置 常用软件历史版本下载 前端常用指令 数组常用方法总结 vue 中 echarts 添加事件 常用正则
element-ui NavMenu 多级嵌套封装
一丝心情 · 2023-05-17 · via 博客园 - 一丝心情
  1. 安装依赖引用依赖
    /* 安装依赖 */
    // 安装element-ui
    npm i element-ui -S
    // 安装vue-fragment
    npm i -s vue-fragment
    
    
    /* main.js 引入 */
    import Vue from 'vue';
    // 引用element-ui
    import ElementUI from 'element-ui';
    import 'element-ui/lib/theme-chalk/index.css';
    // 引用vue-fragment
    import { Plugin } from 'vue-fragment'
    Vue.use(Plugin)
  2. SidebarItem 组件封装
    <template>
        <fragment v-if="!item.hidden">
            <el-submenu
                v-if="item.children && item.children.length"
                :index="item.label"
            >
                <template slot="title">
                    <i :class="item.icon"/>
                    {{ item.label }}
                </template>
    
                <sidebar-item
            v-for="child in item.children"
                    @item-click="(i) => $emit('item-click', i)"
            :key="child.path"
            :item="child"
          />
            </el-submenu>
    
            <el-menu-item
                v-else
                :index="item.url"
                @click="$emit('item-click', item)">
                <i :class="item.icon"/>
                {{ item.label }}
            </el-menu-item>
        </fragment>
    </template>
    
    <script>
    export default {
      name: 'SidebarItem',
      props: {
        item: {
          type: Object,
          required: true
        }
      },
      methods: {
        handleItemClick (item) {
          this.$emit('item-click', item)
        }
      }
    }
    </script>
  3. SidebarItem组件应用
    <template>
      <el-menu :default-active="activeIndex" unique-opened @select="handleLoadUrl">
        <sidebar-item v-for="item in menuList" :key="item.path" :item="item" @item-click="handleItemClick"/>
      </el-menu>
    </template>
    
    <script>
    import SidebarItem from './sidebarItem.vue'
    export default {
      components: { SidebarItem },
      data () {
        return {
          activeIndex: '/1',
          menuList: [
            {
              label: '一级菜单1',
              icon: 'el-icon-s-help',
              url: '/1'
            },
            {
              label: '一级菜单2',
              icon: 'el-icon-s-help',
              url: '/2',
              children: [
                  { label: '二级菜单2.1', url: '/2/1' }
              ]
            }
          ]
        }
      },
      watch: {
        $route: {
          handler: function (to, from) {
            this.$nextTick(() => {
              if (to) {
                this.activeIndex = to.path
              }
            })
            // console.log(to, from)
          },
          immediate: true
        }
      },
      created () {
      },
      mounted () {},
      methods: {
        handleLoadUrl (index, indexPath) {
          // console.log(index, indexPath, 5566)
        },
        handleItemClick (item) {
          const { url, label } = item
          this.activeIndex = url || label
          this.$router.push(this.activeIndex)
        }
      }
    }
    </script>
  4. .....