reactjs后台管理系统搭建

news2024/10/5 12:40:38

1 通过yarn 模板创建reactjs项目

yarn create vite reactjs-antdesign-admin --template react-ts

2 基础路由测试

定义一个router/index.tsx,里面定义路由组件

const Router: React.FC = () => {
  return (
    <HashRouter>
      <Switch>
        <Route path="/" component={Welcome} exact/>
        <Route path="/user" >
          <Switch>
            <Route path='/user' render={() => <Redirect to="/user/login" />} exact />
            <Route path='/user/login' component={Login} />
          </Switch>
        </Route>
        <Route path="/welcome"component={Welcome}/>
        <Route path="/admin">
          <Route path='/admin' render={() => <Redirect to="/admin/sub-page" />} exact />
          <Route path='/admin/sub-page' component={Admin}/>
        </Route>
        <Route path="/list" component={TableList} />
        <Route path="*" component={NotPage} />
      </Switch>
      </HashRouter>
  );
};

 在App.tsx中使用路由组件

function App() :ReactElement{
  return (
    <div className="App">
      <BrowserRouter>
      <Provider store={store}>
            <Suspense fallback={<div>wwww</div>}>
              <ErrorBoundary>
                <Router />
              </ErrorBoundary>
            </Suspense>
        </Provider>
        </BrowserRouter>
    </div>
  );
}
export default App;

3 管理系统搭建

3.1 路由结构体

export interface AppRouter {
  path: string //路由路径
  name?: string //菜单名称
  component?: //组件
    | React.ComponentType<RouteComponentProps<any>>
    | React.ComponentType<any>
  auth?: boolean //是否认证
  isHidden?: boolean //菜单是否隐藏
  icon?: any //菜单图标
  redirectUrl?: string //菜单是否重定向
  routes?: AppRouter[] //子路由
}

3.2 路由定义

router/index.tsx 

const routers: Array<AppRouter> = [
  {
    path: '/',
    name: '主界面',
    component: Welcome,
    auth: true,
    icon: <CrownFilled />,
  },
  {
    path: '/user',
    name: '用户管理',
    redirectUrl: '/user/login',
    isHidden: true,
    icon: <CrownFilled />,
    routes: [
      {
        path: '/user/login',
        component: Login,
        icon: <CrownFilled />,
      },
    ],
  },
  {
    name: '欢迎界面',
    path: '/welcome',
    component: Welcome,
    icon: <CrownFilled />,
    auth: true,
  },
  {
    name: '管理员',
    path: '/admin',
    redirectUrl: '/admin/sub-page',
    icon: <CrownFilled />,
    routes: [
      {
        name: '管理子页',
        path: '/admin/sub-page',
        icon: <CrownFilled />,
        component: Admin,
      },
    ],
  },
  {
    name: '列表页面',
    path: '/list',
    component: TableList,
    icon: <CrownFilled />,
    auth: true,
  },
  {
    path: '*',
    component: NotPage,
    auth: true,
    isHidden: true,
  },
]

3.3 App布局文件

function App(): ReactElement {
  return (
    <>
      <BrowserRouter>
        <Provider store={store}>
          <Suspense fallback={<PageLoading></PageLoading>}>
            <ErrorBoundary>
              <FrontendRouter routerConfig={WithAppRouters(routers)} />
            </ErrorBoundary>
          </Suspense>
        </Provider>
      </BrowserRouter>
    </>
  )
}
export default App

FontendRouter是处理认证的信息,WithAppRouters是重新构建真正的路由信息列表

FontendRouter主要是导入路由包含在

<MainLayout>
                    <Route
                      path={pathname}
                      component={targetRouterConfig.component}
                      exact
                    />
                  </MainLayout>

3.4 MainLayout布局文件定义

这里直接去ProLayout - 高级布局 - ProComponents (ant.design) 选择需要的布局

const MainLayout: React.FC<{ children: ReactNode }> = (props) => {
  const [settings, setSetting] = useState<Partial<ProSettings> | undefined>({
    fixSiderbar: true,
    layout: 'mix',
    splitMenus: false,
  })

  const [pathname, setPathname] = useState('/')
  const [num, setNum] = useState(40)
  if (typeof document === 'undefined') {
    return <div />
  }
  return (
    <div
      id="test-pro-layout"
      style={{
        height: '100vh',
        overflow: 'auto',
      }}
    >
      <ProConfigProvider hashed={false}>
        <ConfigProvider
          getTargetContainer={() => {
            return document.getElementById('test-pro-layout') || document.body
          }}
        >
          <ProLayout
            prefixCls="my-prefix"
            bgLayoutImgList={[
              {
                src: 'https://img.alicdn.com/imgextra/i2/O1CN01O4etvp1DvpFLKfuWq_!!6000000000279-2-tps-609-606.png',
                left: 85,
                bottom: 100,
                height: '303px',
              },
              {
                src: 'https://img.alicdn.com/imgextra/i2/O1CN01O4etvp1DvpFLKfuWq_!!6000000000279-2-tps-609-606.png',
                bottom: -68,
                right: -45,
                height: '303px',
              },
              {
                src: 'https://img.alicdn.com/imgextra/i3/O1CN018NxReL1shX85Yz6Cx_!!6000000005798-2-tps-884-496.png',
                bottom: 0,
                left: 0,
                width: '331px',
              },
            ]}
            {...defaultProps}
            location={{
              pathname,
            }}
            token={{
              header: {
                colorBgMenuItemSelected: 'rgba(0,0,0,0.04)',
              },
            }}
            avatarProps={{
              src: 'https://gw.alipayobjects.com/zos/antfincdn/efFD%24IOql2/weixintupian_20170331104822.jpg',
              size: 'small',
              title: '七妮妮',
              render: (props, dom) => {
                return (
                  <Dropdown
                    menu={{
                      items: [
                        {
                          key: 'logout',
                          icon: <LogoutOutlined />,
                          label: '退出登录',
                        },
                      ],
                    }}
                  >
                    {dom}
                  </Dropdown>
                )
              },
            }}
            actionsRender={(props) => {
              if (props.isMobile) return []
              if (typeof window === 'undefined') return []
              return [
                <InfoCircleFilled key="InfoCircleFilled" />,
                <QuestionCircleFilled key="QuestionCircleFilled" />,
                <GithubFilled key="GithubFilled" />,
              ]
            }}
            menuFooterRender={(props) => {
              if (props?.collapsed) return undefined
              return (
                <div
                  style={{
                    textAlign: 'center',
                    paddingBlockStart: 12,
                  }}
                >
                  <div>© 2021 Made with love</div>
                  <div>by Ant Design</div>
                </div>
              )
            }}
            onMenuHeaderClick={(e) => console.log(e)}
            menuItemRender={(item, dom) => (
              <div
                onClick={() => {
                  setPathname(item.path || '/welcome')
                }}
              >
                {dom}
              </div>
            )}
            {...settings}
          >
            {props.children}
          </ProLayout>
        </ConfigProvider>
      </ProConfigProvider>
    </div>
  )
}
export default MainLayout

prelayout需要配置:_defaultProps.tsx

import { CrownFilled } from '@ant-design/icons'
import { AppRouter } from '../FrontendAuth'
import { routers } from '@/router'
//将默认的路由配置生成prelayout需要的
export function WithAppRouters(appRouters1: AppRouter[]): AppRouter[] {
  const appRouterFun = (appRouters2: AppRouter[]): AppRouter[] => {
    const newAppRouters: AppRouter[] = []
    for (const appRouter of appRouters2) {
      if (!appRouter.isHidden) {
        newAppRouters.push(appRouter)
        if (appRouter.routes && appRouter.routes.length > 0) {
          appRouter.routes = WithAppRouters(appRouter.routes)
        }
      }
    }
    return newAppRouters
  }
  return appRouterFun(appRouters1)
}
export default {
  route: {
    path: '/',
    routes: WithAppRouters(routers),
  },
  location: {
    pathname: '/',
  },
}

4 显示结果

源码:liu289747235/reactjs-antdesign-admin

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1638740.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Edge浏览器使用心得与深度探索

&#x1f90d; 前端开发工程师、技术日更博主、已过CET6 &#x1f368; 阿珊和她的猫_CSDN博客专家、23年度博客之星前端领域TOP1 &#x1f560; 牛客高级专题作者、打造专栏《前端面试必备》 、《2024面试高频手撕题》 &#x1f35a; 蓝桥云课签约作者、上架课程《Vue.js 和 E…

信息时代的智慧导航:高效搜索、信息筛选与信任构建的全面指南!

文章目录 一、高效搜索&#xff1a;快速定位目标信息的秘诀二、信息筛选&#xff1a;去伪存真&#xff0c;找到有价值的信息三、信任构建&#xff1a;深入了解与直接沟通《搜索之道&#xff1a;信息素养与终身学习的新引擎》亮点内容简介目录获取方式 随着科技的飞速发展&#…

前端基础学习html(2)

目录 表格标签&#xff1a; 列表标签&#xff1a; 表格标签&#xff1a; <!-- 表格基本架构 --><!-- tr表示一行&#xff0c;td表示一行内单元格 --><!--th为第一行表头加粗居中显示 --><table border"1"><thead><tr><th&g…

用Stream流方式合并两个list集合(部分对象属性重合)

一、合并出共有部分 package com.xu.demo.test;import java.util.Arrays; import java.util.List; import java.util.stream.Collectors;public class ListMergeTest1 {public static void main(String[] args) {List<User> list1 Arrays.asList(new User(1, "Alic…

【linux学习指南】linux指令与实践文件编写

文章目录 &#x1f4dd;前言&#x1f320; linux目录结构&#x1f309;linux命令介绍 &#x1f320;pwd命令&#x1f309;mkdir指令&#xff08;重要&#xff09; &#x1f320;cd 指令&#x1f309;touch指令 &#x1f320;rmdir指令 && rm 指令&#xff08;重要&…

nginx--配置文件

组成 主配置文件&#xff1a;nginx.conf 子配置文件&#xff1a;include conf.d/*.conf 协议相关的配置文件&#xff1a;fastcgi uwsgi scgi等 mime.types&#xff1a;⽀持的mime类型&#xff0c;MIME(Multipurpose Internet Mail Extensions)多用途互联⽹网邮件扩展类型&…

KUKA机器人KR3 R540维护保养——涂润滑脂

KUKA机器人在保养时少不了润滑脂&#xff0c;不同型号的机器人需要的润滑脂类型也不一样&#xff0c;保养时注意选用合适的润滑脂。本篇文章以KUKA机器人KR3 R540为例&#xff0c;在轴盖板 A2、A3、A5 的内侧涂上润滑脂。 一、涂润滑脂的作用 拆开机器人一个轴的盖板&am…

链表面试题2

1&#xff0c;合并两个有序链表 我们先定义一个虚拟节点newH&#xff0c; 然后按照上图所走&#xff0c;但是当其中一个链表走空时&#xff0c;我们只需返回另一个链表即可 class Solution {public ListNode mergeTwoLists(ListNode headA, ListNode headB) {ListNode newhead…

python基础语法--函数

一、函数概述 函数就是执行特定任务完成特定功能的一段代码。可以在程序中将某一段代码定义成函数&#xff0c;并指定一个函数名和接收的输入&#xff08;参数&#xff09;&#xff0c;这样就可以在程序的其他地方通过函数名多次调用并执行该段代码了。 每次调用执行后&#…

Mybatis-Plus学习:快速入门、核心功能、扩展功能、插件功能

文章目录 MybatisPlus快速入门快速开始常见注解常见配置 核心功能条件构造器&#xff08;Wrapper&#xff09;自定义SQLService接口基本用法基础业务接口复杂业务接口Lamda查询Lamda更新批量新增 扩展功能代码生成代码生成器快速开发插件 静态工具逻辑删除枚举处理器JSON处理器…

机器人系统ros2-开发实践04-ROS 2 启动文件管理大型项目的最佳实践

机器人上的大型应用通常涉及多个互连的节点&#xff0c;每个节点可以有许多参数。海龟模拟器中模拟多只海龟就是一个很好的例子。海龟模拟由多个海龟节点、世界配置以及 TF 广播器和监听器节点组成。在所有节点之间&#xff0c;存在大量影响这些节点的行为和外观的 ROS 参数。 …

【C++】哈希的应用---位图

目录 1、引入 2、位图的概念 3、位图的实现 ①框架的搭建 ②设置存在 ③设置不存在 ④检查存在 ​4、位图计算出现的次数 5、完整代码 1、引入 我们可以看一道面试题 给40亿个不重复的无符号整数&#xff0c;没排过序。给一个无符号整数&#xff0c;如何快速判断一个数…

罗宾斯《管理学》第15版笔记/课后习题/考研真题答案

第Ⅰ篇 管理导论 第1章 工作场所中的管理者和你 1.1 知识结构导图 1.2 考点难点归纳 1.3 课后习题详解 1.4 考研真题详解 附加模块一 管理史 知识结构导图 考点难点归纳 课后习题详解 考研真题详解 第2章 决 策 2.1 知识结构导图 2.2 考点难点归纳 2.3 课后习题详解…

C++string类使用大全

目录 温馨提示&#xff1a;这篇文章有约两万字 什么是string类&#xff1f; 一. 定义和初始化string对象 1.string的构造函数的形式&#xff1a; 2.拷贝赋值运算符 3.assign函数 二.string对象上的操作 1.读写string对象 2.读取未知数量的string对象 3.使用getline …

STM32中断系统详解

系列文章目录 STM32单片机系列专栏 C语言术语和结构总结专栏 文章目录 1. 中断基本概念 2. STM32中断 3. NVIC的基本组件 3.1 NVIC的基本组件 3.2 NVIC的优先级 4. EXTI外部中断 4.1 基本概念 4.2 基本结构 5. AFIO 1. 中断基本概念 中断&#xff08;Interrupt&…

信息技术内涵及意义

一、信息技术及其演进趋势 &#xff08;一&#xff09;信息技术概况概念 信息技术&#xff08;Information Technology&#xff0c;IT&#xff09;指“应用在信息加工和处理中的科学、技术与工程的训练方法与管理技巧&#xff1b;上述方法和技巧的应用&#xff1b;计算机及其…

mac idea 下载spring 源码遇到的问题

一、Kotlin: warnings found and -Werror specified 这个问题网上看了很多文章多说是缺少cglib、objenesis包。然后执行了 实际还是没有什么用 解决&#xff1a; 最后自己看了一下前面一个警告。说的就是版本太低。所以我觉得是这个前置问题导致的 然后搜索了改这个Kotlin版本…

《ElementUI 基础知识》el-tabs header 监听鼠标中键滚动时左右滑动(ElementPlus同样适用)

前言 收到需求&#xff0c;可监听 el-tabs 头在鼠标 hover 时。滑动鼠标中键&#xff0c;可左右滑动&#xff01; 效果 鼠标中键上下滑动时&#xff1b;向上滑&#xff0c;向左移动&#xff1b;向下滑&#xff0c;向右移动&#xff1b; 实现 代码56 - 60行&#xff0c;添加…

前端开发框架Vue

版权声明 本文原创作者&#xff1a;谷哥的小弟作者博客地址&#xff1a;http://blog.csdn.net/lfdfhl Vue概述 Vue.js&#xff08;简称Vue&#xff09;是由尤雨溪&#xff08;Evan You&#xff09;创建并维护的一款开源前端开发框架。Vue以其轻量级、易上手和高度灵活的特点&…