【问题标题】:How to add link in sidebar of ant design (version >=4.20.0)ant design侧边栏添加链接的方法(版本>=4.20.0)
【发布时间】:2023-01-21 13:04:12
【问题描述】:

我搜索并尝试了不同的方法来在 ant design 的侧边栏中应用链接。但徒劳无功。 在上一个版本中,我们正在应用这样的链接

<Menu.SubMenu key="SubMenu" icon={<SettingOutlined />}>
  <Menu.Item key="two" icon={<AppstoreOutlined />}>
    <Link to="">Navigation Two</Link>
  </Menu.Item>
</Menu.SubMenu>

但是现在他们已经将其更改为基于功能的。像这样的东西

<Menu
  mode="inline"
  openKeys={openKeys}
  onOpenChange={onOpenChange}
  style={{ width: 256 }}
  items={items}
/>

现在我尝试了几种方法来将链接应用于代码。但它们无法正常工作。如果有人可以提供帮助,请提供帮助。

antd链接:https://ant.design/components/menu/

【问题讨论】:

    标签: javascript reactjs frontend antd


    【解决方案1】:

    那么你可以做的是在项目数组的标签键中添加导航,然后你的代码将像以前一样工作

    const items: MenuProps['items'] = [
      {
        label: 'Navigation One',
        key: 'mail',
        icon: <MailOutlined />,
      },
      {
        label: (
          <a href="https://ant.design" target="_blank" rel="noopener noreferrer">
            Navigation Two
          </a>
        ),
        key: 'alipay',
      },
    ];
    

    【讨论】:

    • 谢谢你,兄弟。答案一直都在那里,但我看不到。
    【解决方案2】:

    使用这个反应组件

    • 添加&lt;Link /&gt;组件
    • 显示 3 个级别
    • 可以检查权限
    • 完全自定义设置
    import React, { useContext, useState } from 'react'
    import { HomeOutlined, SafetyOutlined } from '@ant-design/icons';
    import { Menu, Layout } from 'antd';
    import { Link } from "react-router-dom";
    import { AuthContext } from 'context/auth-context'
    import { PrivateLayoutContext } from 'context/private-layout-context'
    const { Sider } = Layout;
    
    
    function LeftSideBar() {
        const { permissions } = useContext(AuthContext);
        const { leftSideBarCollapsed } = useContext(PrivateLayoutContext);
    
        const items = [
            {
                label: 'Home',
                path: '/',
                key: 'home',
                icon: <HomeOutlined />,
            },
            {
                label: 'Access Control',
                key: 'access-control',
                icon: <SafetyOutlined />,
                children: [
                    {
                        label: 'User',
                        path: '/access-control/user',
                        key: 'user',
                        permission: 'user list',
                    },
                    {
                        label: 'Role',
                        path: '/access-control/role',
                        key: 'role',
                        permission: 'role list',
                    }
                ]
            },
            {
                label: 'Configuration',
                key: 'configuration',
                icon: <SafetyOutlined />,
                children: [
                    {
                        label: 'Inventory',
                        key: 'inventory',
                        children: [
                            {
                                label: 'Inventory 1',
                                path: '/configuration/inventory',
                                key: 'inventory-1',
                                permission: 'inventory list',
                            },
                            {
                                label: 'Inventory 2',
                                path: '/configuration/inventory-2',
                                key: 'inventory-2',
                                permission: 'inventory list',
                            },
                        ]
    
                    },
                    {
                        label: 'Customer',
                        path: '/configuration/customer',
                        key: 'customer',
                    }
                ]
            },
        ]
    
        const onClick = (e) => {
            console.log('click', e);
        };
    
    
        return (
            <Sider trigger={null} collapsible collapsed={leftSideBarCollapsed} >
    
                {/* This menu is designed to be used for displaying 3 levels */}
                <Menu
                    onClick={(e) => onClick(e)}
                    style={{
                        height: '100vh',
                    }}
                    mode="vertical"
                    items={
                        // level 1 = root level
                        items.map((l1_item, index) => {
                            // console.log(l1_item?.permission, permissions, permissions?.includes(l1_item?.permission));
                            return {
                                ...l1_item,
                                label: <Link to={l1_item?.path}>{l1_item?.label}</Link>,
                                // level 2
                                children: l1_item?.children?.map((l2_item, l2_index) => {
                                    // if (l2_item) has permission then check permission exist in permissions array, otherwise return
                                    let return_status = 0;
                                    if (l2_item?.permission) {
                                        if (permissions?.includes(l2_item?.permission)) {
                                            return_status = 1;
                                        }
                                    }
                                    else {
                                        return_status = 1;
                                    }
                                    return return_status && {
                                        ...l2_item,
                                        label: <Link to={l2_item?.path}>{l2_item?.label}</Link>,
                                        // level 3
                                        children: l2_item?.children?.map((l3_item, l3_index) => {
                                            // if (l3_item) has permission then check permission exist in permissions array, otherwise return
                                            let return_status = 0;
                                            if (l3_item?.permission) {
                                                if (permissions?.includes(l3_item?.permission)) {
                                                    return_status = 1;
                                                }
                                            }
                                            else {
                                                return_status = 1;
                                            }
                                            return return_status && {
                                                ...l3_item,
                                                label: <Link to={l3_item?.path}>{l3_item?.label}</Link>
                                            }
                                        })
                                    }
                                })
                            }
                        })
                    }
                >
    
    
                </Menu>
    
            </Sider>
        )
    };
    export default LeftSideBar;
    

    【讨论】:

      猜你喜欢
      • 2022-07-02
      • 1970-01-01
      • 2021-02-22
      • 2021-12-04
      • 2014-11-26
      • 2016-05-15
      • 2020-06-03
      • 2018-05-21
      • 2020-08-01
      相关资源
      最近更新 更多