要给 Ant Design 的二级导航添加选中的背景色,可以使用 selectedKeys 属性来指定当前选中的菜单项。具体的步骤如下:

  1. 首先,在你的组件中引入 MenuSubMenu 组件。
import { Menu } from 'antd';
const { SubMenu } = Menu;
  1. 在组件的状态中添加一个 current 属性来保存当前选中的菜单项的 key。
state = {
  current: 'sub1',
};
  1. Menu 组件中使用 selectedKeys 属性将 current 的值传递给 Menu 组件。
<Menu
  selectedKeys={[this.state.current]}
  mode="horizontal"
  onClick={this.handleClick}
>
  {/* 在这里添加你的菜单项 */}
</Menu>
  1. 添加 handleClick 方法来更新 current 的值。
handleClick = e => {
  console.log('click ', e);
  this.setState({
    current: e.key,
  });
};
  1. SubMenu 组件中使用 className 属性来添加选中的背景色。
<SubMenu
  key="sub1"
  title={
    <span className="submenu-title-wrapper">
      Navigation One - Submenu
    </span>
  }
  className={this.state.current === 'sub1' ? 'ant-menu-submenu-selected' : ''}
>
  {/* 在这里添加你的子菜单项 */}
</SubMenu>
  1. 最后,在 CSS 文件中添加选中的背景色。
.ant-menu-submenu-selected {
  background-color: #1890ff;
}

这样就可以给 Ant Design 的二级导航添加选中的背景色了

antd 二级导航如何加选中的背景色

原文地址: http://www.cveoy.top/t/topic/i1Y1 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录