当前位置:   article > 正文

React+ant-design搭建后台管理系统【第一章】

React+ant-design搭建后台管理系统【第一章】

项目创建安装以及配置:

# 创建react项目

1.安装react依赖

cnpm i yarn -g

2.创建项目,项目名称不要有大写字母

npx create-react-app 自定义项目名称

3.//进入项目目录后,启动项目

npm run start 或者 npm start

# 安装项目插件

1.安装AntUI组件库

npm install antd --save

2.安装react-redux

npm install react-redux @reduxjs/toolkit -S

3.安装 react-router-dom@5

npm install react-router-dom@5

4.安装请求axios

 

 

 这里简单说明一下项目架构如图:

 

这里默认初始化页面配置全局

  1. 1.这里使用Ant组件库中的菜单栏配置切换路由:
  2. import React, { Component } from 'react';
  3. import { Breadcrumb, Avatar, Layout, Dropdown, Menu, Image } from "antd";
  4. import { UserOutlined, DownOutlined, LaptopOutlined, NotificationOutlined } from '@ant-design/icons';
  5. // import ComHeader from './common/Header'
  6. import "../assets/css/home.css"
  7. import List from '../pages/List'
  8. import Mine from '../pages/Mine'
  9. import Login from '../pages/Login'
  10. import Sex from '../pages/Sex'
  11. import { BrowserRouter as Router, Route, Link } from "react-router-dom";
  12. const { SubMenu } = Menu;
  13. const { Header, Content, Sider } = Layout;
  14. const menus = (
  15. <Menu
  16. items={[
  17. {
  18. key: '1',
  19. label: (
  20. <a target="_blank" rel="noopener noreferrer" href="https://www.antgroup.com">
  21. 首页
  22. </a>
  23. ),
  24. },
  25. {
  26. key: '2',
  27. label: (
  28. <a target="_blank" rel="noopener noreferrer" href="https://www.aliyun.com">
  29. 退出登录
  30. </a>
  31. ),
  32. },
  33. ]}
  34. />
  35. );
  36. export default class Home extends Component {
  37. constructor(props) {
  38. super(props);
  39. this.state = {
  40. userInfo: {}
  41. };
  42. this.init()
  43. }
  44. init() {
  45. // this.props.history.push("/List")
  46. }
  47. render() {
  48. // let { match } = this.props;
  49. return (
  50. <Router>
  51. <div>
  52. <div>
  53. <Layout>
  54. <Header className="header" style={{ color: '#fff' }}>
  55. React-Admin-Zrf
  56. {/*导航栏外部组件*/}
  57. {/* <ComHeader/> */}
  58. <Dropdown overlay={menus} trigger={['click']}>
  59. <a onClick={(e) => e.preventDefault()}>
  60. <Avatar style={{ backgroundColor: '#87d068'}} gap={15} icon={<UserOutlined />} />
  61. <DownOutlined />
  62. </a>
  63. </Dropdown>
  64. {/* <Image src="https://yxsz.oss-cn-hangzhou.aliyuncs.com/pic/bearPic/93%E7%A8%80%E6%9C%891.jpg" height={40} width={100} alt="" />
  65. <Image src="https://yxsz.oss-cn-hangzhou.aliyuncs.com/pic/bearPic/72%E7%A8%80%E6%9C%892.jpg" height={40} width={100}></Image>
  66. <Image src="https://yxsz.oss-cn-hangzhou.aliyuncs.com/pic/bearPic/496%E7%A8%80%E6%9C%894jpg.jpg" height={40} width={100}></Image>
  67. <Image src="https://yxsz.oss-cn-hangzhou.aliyuncs.com/pic/bearPic/441%E7%A8%80%E6%9C%895jpg.jpg" height={40} width={100}></Image>
  68. <Image src="https://yxsz.oss-cn-hangzhou.aliyuncs.com/pic/bearPic/202%E7%A8%80%E6%9C%893.jpg" height={40} width={100}></Image> */}
  69. </Header>
  70. <Layout>
  71. <Sider width={200} className="site-layout-background">
  72. <Menu
  73. mode="inline"
  74. defaultSelectedKeys={['1']}
  75. defaultOpenKeys={['sub1']}
  76. style={{ height: '100%', borderRight: 0 }}
  77. >
  78. <SubMenu key="sub1" icon={<UserOutlined />} title="业务统计">
  79. <Menu.Item key="1">
  80. <Link to="/List">发送量统计</Link>
  81. </Menu.Item>
  82. <Menu.Item key="2">
  83. <Link to="/Mine">发送记录查询</Link>
  84. </Menu.Item>
  85. <Menu.Item key="3">
  86. <Link to="/Login">短信日志分析</Link>
  87. </Menu.Item>
  88. <Menu.Item key="4">
  89. <Link to="/Sex">费用统计</Link>
  90. </Menu.Item>
  91. </SubMenu>
  92. {/* <SubMenu icon={<LaptopOutlined/>} title="权限信息"></SubMenu> */}
  93. <Menu.Item >
  94. 权限信息
  95. </Menu.Item>
  96. <SubMenu key="sub2" icon={<NotificationOutlined />} title="系统设置"></SubMenu>
  97. </Menu>
  98. </Sider>
  99. <Layout style={{ padding: '0 24px 24px' }}>
  100. {/*面包屑区域*/}
  101. <Breadcrumb style={{ margin: '16px 0' }}>
  102. <Breadcrumb.Item>短信服务</Breadcrumb.Item>
  103. </Breadcrumb>
  104. <Content
  105. className="site-layout-background"
  106. style={{
  107. padding: 24,
  108. margin: 0,
  109. minHeight: '100vh', background: '#fff'
  110. }}
  111. >
  112. {/*内容区域*/}
  113. <Route exact path="/List" component={List} />
  114. <Route exact path="/Mine" component={Mine} />
  115. <Route exact path="/Sex" component={Sex} />
  116. <Route exact path="/Login" component={Login} />
  117. </Content>
  118. </Layout>
  119. </Layout>
  120. </Layout>
  121. </div>
  122. </div>
  123. </Router>
  124. )
  125. }
  126. }

 这里主要配置文件APP.js

  1. import React from 'react';
  2. import {
  3. BrowserRouter as Router,
  4. Route,
  5. Switch,
  6. Redirect
  7. } from 'react-router-dom';
  8. import { connect } from 'react-redux' //引入连接器
  9. import * as userActions from './store/actions/userAction'
  10. import { bindActionCreators } from 'redux';
  11. import Routers from './routes/routerMap'
  12. import NotFound from './pages/NotFound'
  13. function App(props) {
  14. let token = props.token
  15. // console.log('token:', token)
  16. return (
  17. <div>
  18. {
  19. <Router>
  20. <Switch>
  21. {
  22. Routers.map((item, index) => {
  23. return <Route key={index} path={item.path} render={props =>
  24. (!item.auth ? (<item.component {...props} />) : (token ? <item.component {...props} /> : <Redirect to={{
  25. pathname: '/login',
  26. state: { from: props.location }
  27. }} />)
  28. )} />
  29. })
  30. }
  31. {/* 所有错误路由跳转页面 */}
  32. <Route component={NotFound} />
  33. </Switch>
  34. </Router>
  35. }
  36. </div>
  37. );
  38. }
  39. const mapStateToProps = state => {
  40. // console.log(state)
  41. return {
  42. token: state.user.token,
  43. }
  44. }
  45. const mapDispatchToProps = (dispatch) => {
  46. return {
  47. userActions: bindActionCreators(userActions, dispatch)
  48. }
  49. }
  50. export default connect(mapStateToProps, mapDispatchToProps)(App)

 如图所示效果:

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

闽ICP备14008679号