ant-design-pro 动态菜单配置Icon和菜单权限显隐控制

ant-design-pro 修改为动态菜单有很多大佬都给了解决方法,就不多说直接进入怎么配置动态icon!!!!

在配置动态菜单时发现,后台接口返回的路由必须要在config.ts中的routes配置,才能正常显示。这下route都确定好了,route中的icon也是确定的,即使接口数据icon不一样,显示依旧会出问题,会显示icon的文字。

所以我们就要自定义menu的样式了。

首先在BasicLayout.tsx中查看代码,我的是v4版本,用的是proLayout来搭建的菜单框架,github地址:

https://github.com/ant-design/ant-design-pro-layout/blob/HEAD/README.zh-CN.

BasicLayout.tsx中的代码是直接用的defaultDom,如果我们想动态配置icon的话就要修改 proLayout中的

subMenuItemRender和menuItemRender,这两个的具体作用查看git说明

然后修改成下面这样:

修改成自己的样式,这里render只是修改MenuItem中的部分,不用自己加Menu组件.

 

subMenuItemRender={(HeaderViewProps, defaultDom) => {
        return (
          <p>
            <IconFont type={HeaderViewProps.icon} style={{ fontSize: '20px' }} />
            {HeaderViewProps.name}
          </p>
        );
      }}
      menuItemRender={(menuItemProps, defaultDom) => {
        if (menuItemProps.isUrl || menuItemProps.children || !menuItemProps.path) {
          return defaultDom;
        }
        return (
          <Link to={menuItemProps.path}>
            <ul>
              <Menu.Item key={menuItemProps.menuId}>
                <IconFont type={menuItemProps.icon} style={{ fontSize: '20px' }} />
                {menuItemProps.name}
              </Menu.Item>
            </ul>
          </Link>
        );
      }}

我使用的是iconfont,没有使用官方的icon,因为是动态配置icon,如果使用官方的引用,那写的太多了。

推荐一个iconfont库:https://www.iconfont.cn/collections/detail?cid=9402

一般还有控制左侧菜单显隐控制的,我开始用自己数据中的visible在menuItemRender中控制,发现隐藏了它还会有li标签占位!!!会有空白的菜单,这就麻烦了,不能有占位啊!仔细一看proLayout的文档发现了这个:

hideInMenu,我们根据自己的数据来控制这个值的true/false就可以完美解决了,要认真看文档啊!!!

 

怎么在ant-design-pro中使用iconfont,官方文档有:https://ant.design/components/icon-cn/#components-icon-demo-iconfont

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,您可以这样封装: ```vue <template> <a-drawer :visible="visible" :mask-closable="false" :closable="false" :width="420" @close="onClose" > <slot></slot> <div class="drawer-footer"> <a-button style="margin-right: 8px;" @click="onClose">取消</a-button> <a-button type="primary" :loading="submitting" @click="onSubmit"> 提交 </a-button> </div> </a-drawer> </template> <script> import { Drawer, Button } from 'ant-design-vue'; export default { name: 'MyDrawer', components: { 'a-drawer': Drawer, 'a-button': Button, }, props: { visible: { type: Boolean, default: false, }, }, data() { return { submitting: false, }; }, methods: { onClose() { this.$emit('update:visible', false); }, onSubmit() { this.submitting = true; // 处理表单提交逻辑 this.$emit('submit', () => { this.submitting = false; this.onClose(); }); }, }, }; </script> <style> .drawer-footer { display: flex; justify-content: flex-end; align-items: center; height: 56px; border-top: 1px solid #e8e8e8; padding: 0 16px; } </style> ``` 然后在父组件中使用: ```vue <template> <div> <a-button type="primary" @click="visible = true"> 打开抽屉 </a-button> <my-drawer :visible.sync="visible" @submit="onSubmit"> <a-form :form="form"> <a-form-item label="姓名" :label-col="{ span: 6 }" :wrapper-col="{ span: 16 }"> <a-input v-model="form.name" /> </a-form-item> <a-form-item label="年龄" :label-col="{ span: 6 }" :wrapper-col="{ span: 16 }"> <a-input-number v-model="form.age" /> </a-form-item> </a-form> </my-drawer> </div> </template> <script> import MyDrawer from './MyDrawer.vue'; import { Form, Input, InputNumber } from 'ant-design-vue'; export default { components: { MyDrawer, 'a-form': Form, 'a-form-item': Form.Item, 'a-input': Input, 'a-input-number': InputNumber, }, data() { return { visible: false, form: { name: '', age: undefined, }, }; }, methods: { onSubmit(callback) { this.$refs.form.validate(async (valid) => { if (!valid) { return; } // 处理表单提交逻辑 await this.$api.submitForm(this.form); callback(); }); }, }, }; </script> ``` 这样,您就可以在父组件中通过控制 `visible` 来控制抽屉的显隐,并且抽屉中的表单数据可以通过 `submit` 事件提交到父组件中进行处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值