Element组件库动态生成导航

图片来源网络

Element-ui和Vue Router不知道的自行谷歌哈

先来看下怎么样动态加载nav导航

html代码
      <el-menu :default-active="$router.path" router class="el-menu-demo" mode="horizontal" @select="handleSelect" background-color="#545c64" text-color="#fff" active-text-color="#ffd04b">
        <label v-for="(item,index) in navData" :key="index">
          <el-menu-item :index=item.url>
            <i :class=item.icon></i>
            <span slot="title">{{item.name}}</span>
          </el-menu-item>
        </label>
      </el-menu>
js模拟从后台获取的导航数据
      navData: [
            {
                name: "ECharts",
                url: "/Echarts",
                icon: "el-icon-menu"
            },
            {
                name: "菜单选项2",
                url: "2",
                icon: "el-icon-tickets"
            },
            {
                name: "菜单选项3",
                url: "3",
                icon: "el-icon-message"
            },
            {
                name: "菜单选项4",
                url: "4",
                icon: "el-icon-phone-outline"
            },
            {
                name: "菜单选项5",
                url: "5",
                icon: "el-icon-star-off"
            },
            {
                name: "菜单选项6",
                url: "6",
                icon: "el-icon-setting"
            }
        ]

实现效果

截图

复杂导航生成(包含二级菜单和三级菜单)

html代码
<el-menu class="el-menu-vertical-demo" @select="handleOpen" :collapse="isCollapse">
  <label v-for="(item,index) in listNav" :key="index">
    <label v-for="(itemR,indexR) in item.extopts" :key="indexR">
      <label v-for="(iconitem2,iconIndex2) in (indexR).match(/(\S*)@@/)" :key="iconIndex2">
        <label v-if="iconIndex2 > 0">
          <el-menu-item :url="item.url" :index=iconitem2 v-if="item.items.length == 0">
            <label v-for="(iconitem1,iconIndex1) in (indexR).match(/@@(\S*)/)" :key="iconIndex1">
              <label v-if="iconIndex1 > 0">
                <i :class=iconitem1></i>
              </label>
            </label>
            <span slot="title">{{ item.text}}{{iconitem2}}</span>
          </el-menu-item>
          <el-submenu :index=iconitem2 v-else>
            <template slot="title">
              <label v-for="(iconitem3,iconIndex3) in (indexR).match(/@@(\S*)/)" :key="iconIndex3">
                <label v-if="iconIndex3 > 0">
                  <i :class=iconitem3></i>
                </label>
              </label>
              <span slot="title" v-show="false">{{ item.text}}{{iconitem2}}111</span>
            </template>
            <label v-for="(item1,index1) in item.items" :key="index1">
              <label v-for="(itemR1,indexR1) in item1.extopts" :key="indexR1">
                <el-menu-item :url="item1.url" :index=indexR1 v-if="item1.items.length == 0">{{item1.text}}{{indexR1}}</el-menu-item>
                <el-submenu :index=indexR1 v-else>
                  <template slot="title">{{item1.text}}{{indexR1}}</template>
                  <label v-for="(item2,index2) in item1.items" :key="index2">
                    <label v-for="(itemR2,indexR2) in item2.extopts" :key="indexR2">
                      <el-menu-item :url="item2.url" :index=indexR2>{{item2.text}}{{indexR2}}</el-menu-item>
                    </label>
                  </label>
                </el-submenu>
              </label>
            </label>
          </el-submenu>
        </label>
      </label>
    </label>
  </label>
</el-menu>
js模拟从后台获取的导航数据
        listNav: [
            {
                name: "dldt",
                text: "地理地图",
                url: "http://10.156.121.38:9000/gwUap",
                extopts: { "/dldt@@el-icon-location": "" },
                items: [],
                outter: false
            },
            {
                name: "jccx",
                text: "基础查询",
                url: "http://10.156.121.38:9000/gwUap",
                extopts: { "/jccx@@el-icon-search": "" },
                items: [
                    {
                        name: "cxdw",
                        text: "查询定位",
                        url: "http://10.156.121.38:9000/gwUap",
                        extopts: { "/cxdw": "" },
                        items: [
                            {
                                name: "dwcx",
                                text: "定位查询",
                                url: "http://10.156.121.38:9000/gwUap",
                                extopts: { "/dwcx": "" },
                                items: [],
                                outter: false
                            },
                            {
                                name: "dlzhcx",
                                text: "电缆综合查询",
                                url: "http://10.156.121.38:9000/gwUap",
                                extopts: { "/dlzhcx": "" },
                                items: [],
                                outter: false
                            },
                            {
                                name: "scfzsszhcx",
                                text: "生产辅助设施综合查询",
                                url: "http://10.156.121.38:9000/gwUap",
                                extopts: { "/scfzsszhcx": "" },
                                items: [],
                                outter: false
                            },
                            {
                                name: "tdzhcx",
                                text: "通道综合查询",
                                url: "http://10.156.121.38:9000/gwUap",
                                extopts: { "/tdzhcx": "" },
                                items: [],
                                outter: false
                            },
                            {
                                name: "tdnxcx",
                                text: "通道年限查询",
                                url: "http://10.156.121.38:9000/gwUap",
                                extopts: { "/tdnxcx": "" },
                                items: [],
                                outter: false
                            },
                            {
                                name: "jhcx",
                                text: "几何查询",
                                url: "http://10.156.121.38:9000/gwUap",
                                extopts: { "/jhcx": "" },
                                items: [],
                                outter: false
                            }
                        ],
                        outter: false
                    },
                    {
                        name: "dlzycxfx",
                        text: "电缆资源查询分析",
                        url: "http://10.156.121.38:9000/gwUap",
                        extopts: { "/dlzycxfx": "" },
                        items: [
                            {
                                name: "dlmspmcx",
                                text: "电缆埋设剖面查询",
                                url: "http://10.156.121.38:9000/gwUap",
                                extopts: { "/dlmspmcx": "" },
                                items: [],
                                outter: false
                            },
                            {
                                name: "dljpmtcx",
                                text: "电缆井剖面图查询",
                                url: "http://10.156.121.38:9000/gwUap",
                                extopts: { "/dljpmtcx": "" },
                                items: [],
                                outter: false
                            }
                        ],
                        outter: false
                    },
                    {
                        name: "txsc",
                        text: "图形输出",
                        url: "http://10.156.121.38:9000/gwUap",
                        extopts: { "/txsc": "" },
                        items: [
                            {
                                name: "dqtfdy",
                                text: "当前图幅打印",
                                url: "http://10.156.121.38:9000/gwUap",
                                extopts: { "/dqtfdy": "" },
                                items: [],
                                outter: false
                            },
                            {
                                name: "lkdy",
                                text: "拉框打印",
                                url: "http://10.156.121.38:9000/gwUap",
                                extopts: { "/lkdy": "" },
                                items: [],
                                outter: false
                            }
                        ],
                        outter: false
                    }
                ],
                outter: false
            },
            {
                name: "ywgl",
                text: "业务管理",
                url: "http://10.156.121.38:9000/gwUap",
                extopts: { "/ywgl@@el-icon-tickets": "" },
                items: [
                    {
                        name: "zzbd",
                        text: "政治保电",
                        url: "http://10.156.121.38:9000/gwUap",
                        extopts: { "/zzbd": "" },
                        items: [
                            {
                                name: "bdrwd",
                                text: "保电任务单",
                                url: "http://10.156.121.38:9000/gwUap",
                                extopts: {
                                    "/BusinessManagement/politicalProtection/protectTacks/protectTack":
                                        ""
                                },
                                items: [],
                                outter: false
                            }
                        ],
                        outter: false
                    },
                    {
                        name: "gzt",
                        text: "工作台",
                        url: "http://10.156.121.38:9000/gwUap",
                        extopts: { "/gzt": "" },
                        items: [],
                        outter: false
                    },
                    {
                        name: "dmgl",
                        text: "断面管理",
                        url: "http://10.156.121.38:9000/gwUap",
                        extopts: { "/dmgl": "" },
                        items: [],
                        outter: false
                    },
                    {
                        name: "jhgl",
                        text: "计划管理",
                        url: "http://10.156.121.38:9000/gwUap",
                        extopts: { "/jhgl": "" },
                        items: [],
                        outter: false
                    },
                    {
                        name: "xsjcgl",
                        text: "巡视检测管理",
                        url: "http://10.156.121.38:9000/gwUap",
                        extopts: { "/xsjcgl": "" },
                        items: [],
                        outter: false
                    },
                    {
                        name: "yhgl",
                        text: "隐患管理",
                        url: "http://10.156.121.38:9000/gwUap",
                        extopts: { "/yhgl": "" },
                        items: [],
                        outter: false
                    },
                    {
                        name: "fxgk",
                        text: "风险管控",
                        url: "http://10.156.121.38:9000/gwUap",
                        extopts: { "/fxgk": "" },
                        items: [],
                        outter: false
                    },
                    {
                        name: "qxgl",
                        text: "缺陷管理",
                        url: "http://10.156.121.38:9000/gwUap",
                        extopts: { "/qxgl": "" },
                        items: [],
                        outter: false
                    },
                    {
                        name: "gzgl",
                        text: "故障管理",
                        url: "http://10.156.121.38:9000/gwUap",
                        extopts: { "/gzgl": "" },
                        items: [],
                        outter: false
                    },
                    {
                        name: "yxztpj",
                        text: "运行状态评价",
                        url: "http://10.156.121.38:9000/gwUap",
                        extopts: { "/yxztpj": "" },
                        items: [],
                        outter: false
                    },
                    {
                        name: "gcjsgl",
                        text: "工程建设管理",
                        url: "http://10.156.121.38:9000/gwUap",
                        extopts: { "/gcjsgl": "" },
                        items: [],
                        outter: false
                    }
                ],
                outter: false
            },
            {
                name: "jkzz",
                text: "监控主站",
                url: "http://10.156.121.38:9000/gwUap/Monitoring",
                extopts: { "/jkzz@@el-icon-view": "" },
                items: [],
                outter: false
            },
            {
                name: "znxj",
                text: "智能巡检",
                url: "http://10.156.121.38:9000/gwUap",
                extopts: { "/Polling@@el-icon-sort": "" },
                items: [],
                outter: false
            },
            {
                name: "tjfx",
                text: "统计分析",
                url: "http://10.156.121.38:9000/gwUap/Statistics",
                extopts: { "/tjfx@@el-icon-edit": "" },
                items: [],
                outter: false
            },
            {
                name: "zhxx",
                text: "综合信息",
                url: "http://10.156.121.38:9000/gwUap/Synthesize",
                extopts: { "/zhxx@@el-icon-document": "" },
                items: [],
                outter: false
            },
            {
                name: "zbkb",
                text: "指标看板",
                url: "http://10.156.121.38:9000/gwUap/Board",
                extopts: { "/zbkb@@el-icon-news": "" },
                items: [],
                outter: false
            },
            {
                name: "ztt",
                text: "专题图",
                url: "http://10.156.121.38:9000/gwUap",
                extopts: { "/ztt@@el-icon-picture-outline": "" },
                items: [
                    {
                        name: "jkfb",
                        text: "监控分布",
                        url: "http://10.156.121.38:9000/gwUap",
                        extopts: { "/jkfb": "" },
                        items: [],
                        outter: false
                    },
                    {
                        name: "jkgj",
                        text: "监控警告",
                        url: "http://10.156.121.38:9000/gwUap",
                        extopts: { "/jkgj": "" },
                        items: [],
                        outter: false
                    },
                    {
                        name: "tdzyl",
                        text: "通道占用率",
                        url: "http://10.156.121.38:9000/gwUap",
                        extopts: { "/tdzyl": "" },
                        items: [],
                        outter: false
                    },
                    {
                        name: "tdfj",
                        text: "通道分级",
                        url: "http://10.156.121.38:9000/gwUap",
                        extopts: { "/tdfj": "" },
                        items: [],
                        outter: false
                    },
                    {
                        name: "gcjsgl",
                        text: "工程建设管理",
                        url: "http://10.156.121.38:9000/gwUap",
                        extopts: { "/gcjsgl": "" },
                        items: [],
                        outter: false
                    },
                    {
                        name: "yhgl",
                        text: "隐患管理",
                        url: "http://10.156.121.38:9000/gwUap",
                        extopts: { "/yhgl": "" },
                        items: [],
                        outter: false
                    }
                ],

实现效果

截图
截图

这个动态生成的复杂导航因为有些测试需要做,所以菜单名字就一直没有改,不影响实现效果

没看明白或者是有更好方法的小伙伴们可以私信我

这里卿洋
愿喜❤️

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,530评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 86,403评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,120评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,770评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,758评论 5 367
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,649评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,021评论 3 398
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,675评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,931评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,659评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,751评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,410评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,004评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,969评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,203评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,042评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,493评论 2 343

推荐阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明先生_x阅读 15,967评论 3 119
  • 五六公里的路程,每天花一小时走完。坚持了几天之后,会很容易从当初的挣扎心里接受成为一种必不可少的正常行为。路走得多...
    相国618阅读 208评论 0 0
  • 一旦我们发现自己想要学习点新技能、想要提高个人能力时,就会发现原来的无聊再也没有了,取而代之的则是,总觉得找不到时...
    杨刀刀daoker阅读 1,601评论 3 2
  • 天、渐渐的暗了下来。 远处天地相接之处; 乌云压顶、 空气变的格外的凝固; 气流逐渐从地面上升, 仿佛传说中的天在...
    松木叶子阅读 890评论 0 0