AntDsign菜单高亮

AntDsign 版本4.20.2
完整代码在文末

[antd: Menu] children will be removed in next major version. Please use items instead.

原因

<Menu
         mode="inline"
         theme="dark"
         selectedKeys={[selectedKey]}
         style={{ height: '100%', borderRight: 0 }}
          >
        <Menu.Item icon={<HomeOutlined />} key="/">
			    <Link to="/">数据概览</Link>
        </Menu.Item>
       <Menu.Item icon={<DiffOutlined />} key="/article">
             	 <Link to="/article">内容管理</Link>
      </Menu.Item>
        <Menu.Item icon={<EditOutlined />} key="/publish">
                <Link to="/publish">发布文章</Link>
       </Menu.Item>
    </Menu>

使用items替代 可以消除警报

<Menu
         mode="inline"
        theme="dark"
        defaultSelectedKeys={['1']}
       selectedKeys={test()}
       style={{ height: '100%', borderRight: 0 }}
        items={[
                            {
                                key: '1',
                                icon: <HomeOutlined />,
                                label: `数据概览`,
                                onClick: () => { navigate('/'); }
                            },
                            {
                                key: '2',
                                icon: <DiffOutlined />,
                                label: '内容管理',
                                onClick: () => { navigate('/article') }
                            },
                            {
                                key: '3',
                                icon: <EditOutlined />,
                                label: '发布文章',
                                // to: '/publish',
                                onClick: () => { navigate('/publish') }
                            },
                        ]}
                    />

完整代码,(需要自行配置路由)

import { Layout, Menu, Popconfirm } from 'antd'
import {
    HomeOutlined,
    DiffOutlined,
    EditOutlined,
    LogoutOutlined
} from '@ant-design/icons'
import './index.scss'
import { Outlet, useLocation, useNavigate, Link } from 'react-router-dom'

const { Header, Sider } = Layout

const GeekLayout = () => {

    let navigate = useNavigate();

    // // 这里是当前浏览器上的路径地址
    // const selectedKey = useLocation().pathname
    const selectedKey = window.location.pathname
    console.log(selectedKey);

    const highlight= () => {
        if (selectedKey === '/') {
            return ['1']
        } else if (selectedKey === '/article') {

            return ['2']
        } else if (selectedKey === '/publish') {
            return ['3']
        }
    }

    return (
        <Layout>
            <Header className="header">
                <div className="logo" />
                <div className="user-info">
                    <span className="user-name">user.name</span>
                    <span className="user-logout">
                        <Popconfirm title="是否确认退出?" okText="退出" cancelText="取消">
                            <LogoutOutlined /> 退出
                        </Popconfirm>
                    </span>
                </div>
            </Header>
            <Layout>
                <Sider width={200} className="site-layout-background">
                    <Menu
                        mode="inline"
                        theme="dark"
                        defaultSelectedKeys={['1']}
                        // 高亮显示
                        selectedKeys={highlight()}

                        style={{ height: '100%', borderRight: 0 }}
                        items={[
                            {
                                key: '1',
                                icon: <HomeOutlined />,
                                label: `数据概览`,
                                //当点击该标签, 跳转到Home
                                // to: '/',
                                onClick: () => { navigate('/'); }

                            },
                            {
                                key: '2',
                                icon: <DiffOutlined />,
                                label: '内容管理',
                                // to: '/article',
                                onClick: () => { navigate('/article') }
                            },
                            {
                                key: '3',
                                icon: <EditOutlined />,
                                label: '发布文章',
                                // to: '/publish',
                                onClick: () => { navigate('/publish') }

                            },
                        ]}
                    />
                  


                </Sider>
                <Layout className="layout-content" style={{ padding: 20 }}>

                    {/* 二级路由默认页面 */}
                    <Outlet />
                </Layout>
            </Layout>
        </Layout>
    )
}

export default GeekLayout
  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Java精灵儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值