当前位置:   article > 正文

reactjs后台管理系统搭建_react js开发后台管理系统

react js开发后台管理系统

1 通过yarn 模板创建reactjs项目

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

2 基础路由测试

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

  1. const Router: React.FC = () => {
  2. return (
  3. <HashRouter>
  4. <Switch>
  5. <Route path="/" component={Welcome} exact/>
  6. <Route path="/user" >
  7. <Switch>
  8. <Route path='/user' render={() => <Redirect to="/user/login" />} exact />
  9. <Route path='/user/login' component={Login} />
  10. </Switch>
  11. </Route>
  12. <Route path="/welcome"component={Welcome}/>
  13. <Route path="/admin">
  14. <Route path='/admin' render={() => <Redirect to="/admin/sub-page" />} exact />
  15. <Route path='/admin/sub-page' component={Admin}/>
  16. </Route>
  17. <Route path="/list" component={TableList} />
  18. <Route path="*" component={NotPage} />
  19. </Switch>
  20. </HashRouter>
  21. );
  22. };

 在App.tsx中使用路由组件

  1. function App() :ReactElement{
  2. return (
  3. <div className="App">
  4. <BrowserRouter>
  5. <Provider store={store}>
  6. <Suspense fallback={<div>wwww</div>}>
  7. <ErrorBoundary>
  8. <Router />
  9. </ErrorBoundary>
  10. </Suspense>
  11. </Provider>
  12. </BrowserRouter>
  13. </div>
  14. );
  15. }
  16. export default App;

3 管理系统搭建

3.1 路由结构体

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

3.2 路由定义

router/index.tsx 

  1. const routers: Array<AppRouter> = [
  2. {
  3. path: '/',
  4. name: '主界面',
  5. component: Welcome,
  6. auth: true,
  7. icon: <CrownFilled />,
  8. },
  9. {
  10. path: '/user',
  11. name: '用户管理',
  12. redirectUrl: '/user/login',
  13. isHidden: true,
  14. icon: <CrownFilled />,
  15. routes: [
  16. {
  17. path: '/user/login',
  18. component: Login,
  19. icon: <CrownFilled />,
  20. },
  21. ],
  22. },
  23. {
  24. name: '欢迎界面',
  25. path: '/welcome',
  26. component: Welcome,
  27. icon: <CrownFilled />,
  28. auth: true,
  29. },
  30. {
  31. name: '管理员',
  32. path: '/admin',
  33. redirectUrl: '/admin/sub-page',
  34. icon: <CrownFilled />,
  35. routes: [
  36. {
  37. name: '管理子页',
  38. path: '/admin/sub-page',
  39. icon: <CrownFilled />,
  40. component: Admin,
  41. },
  42. ],
  43. },
  44. {
  45. name: '列表页面',
  46. path: '/list',
  47. component: TableList,
  48. icon: <CrownFilled />,
  49. auth: true,
  50. },
  51. {
  52. path: '*',
  53. component: NotPage,
  54. auth: true,
  55. isHidden: true,
  56. },
  57. ]

3.3 App布局文件

  1. function App(): ReactElement {
  2. return (
  3. <>
  4. <BrowserRouter>
  5. <Provider store={store}>
  6. <Suspense fallback={<PageLoading></PageLoading>}>
  7. <ErrorBoundary>
  8. <FrontendRouter routerConfig={WithAppRouters(routers)} />
  9. </ErrorBoundary>
  10. </Suspense>
  11. </Provider>
  12. </BrowserRouter>
  13. </>
  14. )
  15. }
  16. export default App

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

FontendRouter主要是导入路由包含在

  1. <MainLayout>
  2. <Route
  3. path={pathname}
  4. component={targetRouterConfig.component}
  5. exact
  6. />
  7. </MainLayout>

3.4 MainLayout布局文件定义

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

  1. const MainLayout: React.FC<{ children: ReactNode }> = (props) => {
  2. const [settings, setSetting] = useState<Partial<ProSettings> | undefined>({
  3. fixSiderbar: true,
  4. layout: 'mix',
  5. splitMenus: false,
  6. })
  7. const [pathname, setPathname] = useState('/')
  8. const [num, setNum] = useState(40)
  9. if (typeof document === 'undefined') {
  10. return <div />
  11. }
  12. return (
  13. <div
  14. id="test-pro-layout"
  15. style={{
  16. height: '100vh',
  17. overflow: 'auto',
  18. }}
  19. >
  20. <ProConfigProvider hashed={false}>
  21. <ConfigProvider
  22. getTargetContainer={() => {
  23. return document.getElementById('test-pro-layout') || document.body
  24. }}
  25. >
  26. <ProLayout
  27. prefixCls="my-prefix"
  28. bgLayoutImgList={[
  29. {
  30. src: 'https://img.alicdn.com/imgextra/i2/O1CN01O4etvp1DvpFLKfuWq_!!6000000000279-2-tps-609-606.png',
  31. left: 85,
  32. bottom: 100,
  33. height: '303px',
  34. },
  35. {
  36. src: 'https://img.alicdn.com/imgextra/i2/O1CN01O4etvp1DvpFLKfuWq_!!6000000000279-2-tps-609-606.png',
  37. bottom: -68,
  38. right: -45,
  39. height: '303px',
  40. },
  41. {
  42. src: 'https://img.alicdn.com/imgextra/i3/O1CN018NxReL1shX85Yz6Cx_!!6000000005798-2-tps-884-496.png',
  43. bottom: 0,
  44. left: 0,
  45. width: '331px',
  46. },
  47. ]}
  48. {...defaultProps}
  49. location={{
  50. pathname,
  51. }}
  52. token={{
  53. header: {
  54. colorBgMenuItemSelected: 'rgba(0,0,0,0.04)',
  55. },
  56. }}
  57. avatarProps={{
  58. src: 'https://gw.alipayobjects.com/zos/antfincdn/efFD%24IOql2/weixintupian_20170331104822.jpg',
  59. size: 'small',
  60. title: '七妮妮',
  61. render: (props, dom) => {
  62. return (
  63. <Dropdown
  64. menu={{
  65. items: [
  66. {
  67. key: 'logout',
  68. icon: <LogoutOutlined />,
  69. label: '退出登录',
  70. },
  71. ],
  72. }}
  73. >
  74. {dom}
  75. </Dropdown>
  76. )
  77. },
  78. }}
  79. actionsRender={(props) => {
  80. if (props.isMobile) return []
  81. if (typeof window === 'undefined') return []
  82. return [
  83. <InfoCircleFilled key="InfoCircleFilled" />,
  84. <QuestionCircleFilled key="QuestionCircleFilled" />,
  85. <GithubFilled key="GithubFilled" />,
  86. ]
  87. }}
  88. menuFooterRender={(props) => {
  89. if (props?.collapsed) return undefined
  90. return (
  91. <div
  92. style={{
  93. textAlign: 'center',
  94. paddingBlockStart: 12,
  95. }}
  96. >
  97. <div>© 2021 Made with love</div>
  98. <div>by Ant Design</div>
  99. </div>
  100. )
  101. }}
  102. onMenuHeaderClick={(e) => console.log(e)}
  103. menuItemRender={(item, dom) => (
  104. <div
  105. onClick={() => {
  106. setPathname(item.path || '/welcome')
  107. }}
  108. >
  109. {dom}
  110. </div>
  111. )}
  112. {...settings}
  113. >
  114. {props.children}
  115. </ProLayout>
  116. </ConfigProvider>
  117. </ProConfigProvider>
  118. </div>
  119. )
  120. }
  121. export default MainLayout

prelayout需要配置:_defaultProps.tsx

  1. import { CrownFilled } from '@ant-design/icons'
  2. import { AppRouter } from '../FrontendAuth'
  3. import { routers } from '@/router'
  4. //将默认的路由配置生成prelayout需要的
  5. export function WithAppRouters(appRouters1: AppRouter[]): AppRouter[] {
  6. const appRouterFun = (appRouters2: AppRouter[]): AppRouter[] => {
  7. const newAppRouters: AppRouter[] = []
  8. for (const appRouter of appRouters2) {
  9. if (!appRouter.isHidden) {
  10. newAppRouters.push(appRouter)
  11. if (appRouter.routes && appRouter.routes.length > 0) {
  12. appRouter.routes = WithAppRouters(appRouter.routes)
  13. }
  14. }
  15. }
  16. return newAppRouters
  17. }
  18. return appRouterFun(appRouters1)
  19. }
  20. export default {
  21. route: {
  22. path: '/',
  23. routes: WithAppRouters(routers),
  24. },
  25. location: {
  26. pathname: '/',
  27. },
  28. }

4 显示结果

源码:liu289747235/reactjs-antdesign-admin

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/671204
推荐阅读
相关标签
  

闽ICP备14008679号