ANTD 4.21菜单上的React Artign Acrign/Hide项目

发布于 2025-02-09 16:36:42 字数 1657 浏览 0 评论 0原文

有什么方法可以添加不同的类或属性以对齐此 ant Design的2个项目顶级导航菜单?我还可以有条件隐藏一个物品?即,如果有一个用户登录,我想隐藏一个项目。

import { HomeOutlined, SettingOutlined, LoginOutlined, UserAddOutlined} from '@ant-design/icons';
import { Menu } from 'antd';
import { useState } from 'react';
const items = [
    {
        label: 'Home',
        key: 'home',
        icon: <HomeOutlined />,
    },  
    {
        label: 'Username',
        key: 'SubMenu',
        icon: <SettingOutlined />,
        children: [
            {
                type: 'group',
                children: [
                    {
                        label: 'Option 1',
                        key: 'setting:1',
                    },
                    {
                        label: 'Option 2',
                        key: 'setting:2',
                    },
                ],
            },
        ],
    },
    {
        label: 'Login',
        key: 'login',
        icon: <LoginOutlined />
    },
    {
        label: 'Register',
        key: 'register',
        icon: <UserAddOutlined />,
        

    },
];
const Header = () => {
    const [current, setCurrent] = useState('home');
    const onClick = (e) => {
        setCurrent(e.key);
    };
    return (        
        <Menu onClick={onClick} selectedKeys={[current]} mode="horizontal"  items={items} />
    );
};

export default Header;

我正在使用Ant Design的顶级导航菜单: https://ant.design/components/menu/menu/menu/# API

我检查了他们的文档和其他地方,但我似乎无法弄清楚。感谢任何帮助。

谢谢

Is there a way I can add different classes or an attribute to align right 2 items of this ant design top navigation menu? Also how can I hide an item conditionally? IE if there's an user logged in, I want to hide an item.

import { HomeOutlined, SettingOutlined, LoginOutlined, UserAddOutlined} from '@ant-design/icons';
import { Menu } from 'antd';
import { useState } from 'react';
const items = [
    {
        label: 'Home',
        key: 'home',
        icon: <HomeOutlined />,
    },  
    {
        label: 'Username',
        key: 'SubMenu',
        icon: <SettingOutlined />,
        children: [
            {
                type: 'group',
                children: [
                    {
                        label: 'Option 1',
                        key: 'setting:1',
                    },
                    {
                        label: 'Option 2',
                        key: 'setting:2',
                    },
                ],
            },
        ],
    },
    {
        label: 'Login',
        key: 'login',
        icon: <LoginOutlined />
    },
    {
        label: 'Register',
        key: 'register',
        icon: <UserAddOutlined />,
        

    },
];
const Header = () => {
    const [current, setCurrent] = useState('home');
    const onClick = (e) => {
        setCurrent(e.key);
    };
    return (        
        <Menu onClick={onClick} selectedKeys={[current]} mode="horizontal"  items={items} />
    );
};

export default Header;

I'm using ant design's top navigation menu: https://ant.design/components/menu/#API

I checked their documentation and other places but I can't seem to figure it out. I would appreciate any help.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奢华的一滴泪 2025-02-16 16:36:42

我不知道响应是否为时已晚:)
据我所知,对此没有“一线”解决方案。您应该使用条件JSX使用JS制定条件。
例如(在JSX中,在您的React组件中的返回表达式中):

{checkedCondition &&

    <Menu type one .../>
}

{uncheckedCondition &&

    <Menu type two .../>

}  

I don't know if it is too late to respond :)
As far as I know, there is no "one-line" solution for this. You should make the conditions with JS using conditional JSX.
For example (in JSX, in the return expression in your React Component):

{checkedCondition &&

    <Menu type one .../>
}

{uncheckedCondition &&

    <Menu type two .../>

}  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文