vue+antdesign导航菜单动态加载

antdesign侧边栏菜单,需根据后台返回的数据动态加载菜单列表,在循环填充时会遇到子菜单项<a-sub-menu>拆分问题, 查阅官方文档,在单文件递归菜单中,使用函数式组件递归生成菜单

 "menuList": [
        {
            "name": "监控系统",
            "url": "http://192.168.1.100:9999",
            "iconType": "laptop",
        },
        {
            "name": "模块接口",
            "url": null,
            "iconType": "bars",
            "sidebars": [
                {
                    "name": "订单模块",
                    "url": "http://192.168.1.100:8890//swagger-ui.html",
                    "iconType": "italic",
                    "children": []
                }
            ]
        },
        {
            "name": "关于",
            "url": "http://192.168.1.100:9999/about",
            "iconType": "info",
        }
    ]

根据上述后台传递的json数组, 主组件中代码如下:

<template v-for="item in menuList">
  <a-menu-item v-if="!item.sidebars.length" :key="item.name">
     <a-icon :type="item.iconType" />
        <span>{{item.name}}</span>
  </a-menu-item>
  <sub-menu v-else :menu-info="item" :key="item.name"/>
</template>

其中的<sub-menu>由子组件定义,如下:

<template functional>
  <a-sub-menu
    :key="props.menuInfo.name"
  >
    <span slot="title">
      <a-icon :type="props.menuInfo.iconType" /><span>{{ props.menuInfo.name }}</span>
    </span>
    <template v-for="item in props.menuInfo.sidebars">
      <a-menu-item
        :key="item.name"
      >
        <span>{{ item.name }}</span>
      </a-menu-item>
      
    </template>
  </a-sub-menu>
</template>
<script>
export default {
  props: ['menuInfo'],
};
</script>

在<sub-menu>源文件中

<template functional>
  <a-sub-menu
    :key="props.menuInfo.key"
  >
    <span slot="title">
      <a-icon type="mail" /><span>{{ props.menuInfo.title }}</span>
    </span>
    <template v-for="item in props.menuInfo.children">
      <a-menu-item
        v-if="!item.children"
        :key="item.key"
      >
        <a-icon type="pie-chart" />
        <span>{{ item.title }}</span>
      </a-menu-item>
      <sub-menu
        v-else
        :key="item.key"
        :menu-info="item"
      />
    </template>
  </a-sub-menu>
</template>
<script>
export default {
  props: ['menuInfo'],
};
</script>

其中的<sub-menu>中还包含递归组件

转载于:https://my.oschina.net/u/4131669/blog/3048416

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值