react菜单,动态绑定点击事件,菜单分离出去单独的js文件,Ant框架

1、菜单文件treeTop.js

javascript 复制代码
// 顶部菜单
import { AppstoreOutlined, SettingOutlined } from '@ant-design/icons';
// 定义菜单项数据
const treeTop = [
    {
        label: 'Docker管理',
        key: '1',
        icon: <AppstoreOutlined />,
        url:"/docker/index"
      },
      {
        label: '权限管理',
        key: '2',
        icon: <SettingOutlined />,
        children: [
          { label: '管理员', key: '2-1',url:""},
          { label: '退出登录', key: '2-2',url:"/login"},
        ],
      }
];

export default treeTop;

菜单文件

javascript 复制代码
'use client';
import React, { useState } from 'react';
import { Menu,message } from 'antd';
import treeTop from './treeTop.js';

const Menus = () => {
  // 为菜单项添加 onClick 回调(动态绑定)
  treeTop.map((item,index) => {
    if (item.children) {
      item.children.map((child,index2) => {
        if (child.children) {
          // 三级菜单,是按钮,按钮是控制权限,无需加点击事件
          //child.children.map((child2,index3) => {
          //  treeTop[index].children[index2].onClick = () => onClick(child2)
          //})
        }else{
          // 二级菜单
          treeTop[index].children[index2].onClick = () => onClick(child)
        }
      })
    }else{
      // 只有一级菜单
      treeTop[index].onClick = () => onClick(item)
    }
  })
  const [messageApi,contextHolder] = message.useMessage();
  const [current, setCurrent] = useState('1');
  // 菜单点击事件处理函数
  const onClick = e => {
    console.log('click ', e);
    setCurrent(e.key);
    if (e.label === '退出登录') {
      window.location.href = '/login'; // 重定向到登录页面 
    }
    if (e.label === '管理员') {
      messageApi.open({
        type: 'success',
        content: '操作成功',
      });
      messageApi.open({
        type: 'error',
        content: '操作失败',
      });
    }
  };
  // 页面
  return (
    <>
      {/* 提示 */}
      {contextHolder}
      {/* 顶部菜单onClick={onClick} */}
      <Menu selectedKeys={[current]} mode="horizontal" items={treeTop} />
    </>
  );
};
export default Menus;


升级存在url才加点击事件

javascript 复制代码
'use client';
import React, { useState } from 'react';
import { Menu,message } from 'antd';
import treeTop from './treeTop.js';

const Menus = () => {
  // 为菜单项添加 onClick 回调(动态绑定)
  treeTop.map((item,index) => {
    if (item.children) {
      item.children.map((child,index2) => {
        // 没有三级菜单,是按钮,按钮是控制权限,无需加点击事件
        if (child.children) {
          // 三级菜单,是按钮,按钮是控制权限,无需加点击事件
          // child.children.map((child2,index3) => {
          //   treeTop[index].children[index2].onClick = () => onClick(child2)
          // })
        }else{
          // 二级菜单
          // treeTop[index].children[index2].onClick = () => onClick(child)
        }
        // 二级菜单,url不为空,加点击事件
        //if (child.url) {
          // 二级菜单,所以直接加点击事件
        //  treeTop[index].children[index2].onClick = () => onClick(child)
        //}
         // 二级菜单,加点击事件
        treeTop[index].children[index2].onClick = () => onClick(child)
      })
    }
    // 一级菜单,url不为空,加点击事件,如果有children就是按钮权限
    if (item.url) {
      treeTop[index].onClick = () => onClick(item)
    }
  })
  const [messageApi,contextHolder] = message.useMessage();
  const [current, setCurrent] = useState('1');
  // 菜单点击事件处理函数
  const onClick = e => {
    console.log('click ', e);
    setCurrent(e.key);
    if (e.label === '退出登录') {
      window.location.href = '/login'; // 重定向到登录页面 
    }
    if (e.label === '管理员') {
      messageApi.open({
        type: 'success',
        content: '操作成功',
      });
      messageApi.open({
        type: 'error',
        content: '操作失败',
      });
    }
  };
  // 页面
  return (
    <>
      {/* 提示 */}
      {contextHolder}
      {/* 顶部菜单onClick={onClick} */}
      <Menu selectedKeys={[current]} mode="horizontal" items={treeTop} />
    </>
  );
};
export default Menus;
相关推荐
竹林818几秒前
从ethers.js迁移到Viem:我在一个DeFi项目前端重构中踩过的坑
前端·javascript
像我这样帅的人丶你还21 分钟前
从交稿到甩锅预防:AI 前端流水线
前端·ai编程
想想弹幕会怎么做22 分钟前
如何构建一颗可交互的ui树?
前端
程序员陆业聪27 分钟前
我见过的最反直觉的 Android 架构问题:UseCase 越多,项目越烂
前端
Arya_aa33 分钟前
网络:前端向后端发送网络请求渲染在页面上,将EasyMock中的信息用前端vue框架编写代码,最终展示在浏览器
前端·vue.js
LlNingyu35 分钟前
文艺复兴,什么是CSRF,常见形式(一)
前端·安全·web安全·csrf
晓131337 分钟前
React篇——第三章 状态管理之 Redux 篇
前端·javascript·react.js
子兮曰42 分钟前
🚀24k Star 的 Pretext 为何突然爆火:它不是排版库,而是在重写 Web 文本测量
前端·javascript·github
@大迁世界1 小时前
11.在 React.js 中,state 与 props 的差异体现在哪里?
前端·javascript·react.js·前端框架·ecmascript
Giant1001 小时前
🔥前端跨域封神解法:Vite Proxy + Express CORS,一篇搞定所有跨域坑!
前端·javascript·面试