当前位置:   article > 正文

快速搭建React+Ant前端后台管理系统项目:React+AntDesign+ts_搭建react ant-design pro ts 管理后台

搭建react ant-design pro ts 管理后台

2022~07~21,晴,闷热的不行,大早上下地铁就看到了一个身材巨好穿着超短裙的美女,心情美滋滋。废话不多说,上步骤:

1:桌面随便新建一个文件夹,用来存放项目,我的叫my-react

2:打开my-react文件夹,在路径中输入cmd,然后回车

3:然后输入命令:create-react-app project(你想起的项目名称)

4: 此时执行npm start就可以运行项目了,但是虽然项目已经运行成功,但是在项目目录里是找不到webpack配置项的。比如,我们想配置lesssass、配置项目接口请求等,这时我们需要做一个操作:展开项目(eject
注意:这个一个不可逆过程,一旦执行了,就不能回到初始化 。
cd进入到project目录中、执行eject解压配置命令:npm run eject

再看项目结构,此时已多了一些其他目录,展开config目录,里面就有webpack配置文件以及其他各种配置

5:安装less配置

vue-cli 构建的项目默认是不支持 less 的,需要自己添加

运行命令:npm install less less-loader --save-dev

6:安装路由组件router:npm install react-router-dom

7:添加状态管理mobx。mobx是做状态管理的,mobx-react是做数据分片和数据获取

npm install mbox mbox-react。

npm install babel-plugin-transform-decorators-legacy

  • 修改package.json文件中的 babel 参数

 添加下面代码:

  1. "plugins": [
  2. [
  3. "@babel/plugin-proposal-decorators",
  4. {
  5. "legacy": true
  6. }
  7. ]
  8. ]

8:数据请求axois,安装axios库:

 9:引入使用最多的react UI库ant design:npm instal antd

 10:安装ts:npm install --save typescript @types/node @types/react @types/react-dom @types/jest

 一切安装完毕,运行项目:npm start

---------------------------------------------------------分---隔---线-------------------------------------------------------

1:在src文件夹下新建api、components、pages、router文件夹

2:在pages新建login和index文件夹,分别新建login.js和index.js文件

index.js代码:

  1. import React, { Component } from "react";
  2. import { Layout, Switch, Menu } from "antd";
  3. // import { RenderRoutes } from "../../router/utils";
  4. import { Link } from "react-router-dom";
  5. // require("./index.less");
  6. const { Header, Content, Sider } = Layout;
  7. const { SubMenu } = Menu;
  8. class Index extends Component {
  9. state = {
  10. collapsed: false,
  11. theme: "dark",
  12. };
  13. onCollapse = (collapsed) => {
  14. console.log(collapsed);
  15. this.setState({ collapsed });
  16. };
  17. changeTheme = (value) => {
  18. this.setState({
  19. theme: value ? "dark" : "light",
  20. });
  21. };
  22. render() {
  23. const { collapsed } = this.state;
  24. let { routes } = this.props;
  25. console.log(routes);
  26. return (
  27. <Layout style={{ minHeight: "100vh" }}>
  28. <Sider
  29. theme={this.state.theme}
  30. collapsible
  31. collapsed={collapsed}
  32. onCollapse={this.onCollapse}
  33. >
  34. <div className="logo" />
  35. <Menu
  36. theme={this.state.theme}
  37. defaultSelectedKeys={["/user/experience"]}
  38. defaultOpenKeys={["sub1"]}
  39. mode="inline"
  40. >
  41. <Menu.Item key="1">
  42. <Link to="/user/WorkBench">工作台</Link>
  43. </Menu.Item>
  44. <SubMenu key="sub1" title="用户管理">
  45. <Menu.Item key="/user/experience">
  46. <Link to="/user/experience">体验用户</Link>
  47. </Menu.Item>
  48. <Menu.Item key="4">VIP用户</Menu.Item>
  49. <Menu.Item key="5">
  50. <Link to="/user/Details">用户详情</Link>
  51. </Menu.Item>
  52. </SubMenu>
  53. </Menu>
  54. </Sider>
  55. <Layout className="site-layout">
  56. <Header className="ant-layout-header" style={{ background: "#fff" }}>
  57. </Header>
  58. <Content style={{ margin: "16px 16px" }}>
  59. {/* <RenderRoutes routes={routes}></RenderRoutes> */}
  60. </Content>
  61. </Layout>
  62. </Layout>
  63. );
  64. }
  65. }
  66. export default Index;

 login.js代码:

  1. import React, { Component } from "react";
  2. // require("./index.less");
  3. class Login extends Component {
  4. render() {
  5. return (
  6. <div className="main-page">
  7. 登录页
  8. </div>
  9. );
  10. }
  11. }
  12. export default Login ;

在这个步骤有可能会报如下错误,不要慌

原因:这些报错原因均为'Switch' 和'Redirect' 是react-router 5版本的接口,在项目中的package.json中查看你的react-router-dom版本,版本是"react-router-dom": "^6.x.x"。新版并且已经将Switch改为Routes。 

解决办法有二:

1.将所有 Switch 改为 Routes,Redirect 改为 Navigate ,withRouter改为 useNavigate 

(vscode中,按快捷键:CTRL+F及可查找替换)

2.卸载新版本,再安装5的版本

  1. npm uninstall react-router-dom

  2. npm install react-router-dom@5

 我直接使用的第二个,卸载了新版本,重新装了5的版本,就没问题了

 3:在router文件夹下新建router.js和allocation.js

 router.js代码

  1. import Login from '../pages/login/login'
  2. import Index from '../pages/index/index'
  3. // 菜单相关路由
  4. export const menus = []
  5. //登录、首页、404路由
  6. export const main = [
  7. { path: '/login', exact: true, name: '登录', component: Login },
  8. { path: '/', name: '首页', component: Index, routes: menus },
  9. ]
  10. export const routerExport = {
  11. main, menus
  12. }

allocation.js代码:

  1. import React from 'react';
  2. import { Route, Link, Switch, Redirect } from "react-router-dom";
  3. export const SubRoutes = route => (<Route
  4. path={route.path}
  5. exact={route.exact}
  6. render={props =>{
  7. return (
  8. route &&( route.Redirect ? (<Redirect to={route.Redirect}></Redirect>) :
  9. (<route.component {...props} routes={route.routes} />))
  10. )
  11. }}
  12. />
  13. );
  14. export const RenderRoutes = ({routes}) => {return <Switch> {(routes.map((route, i) => <SubRoutes key={i} {...route} />))} </Switch> };

4:到app.js配置

app.js代码

  1. // import logo from './logo.svg';
  2. import './App.css';
  3. import 'antd/dist/antd.css'; // or 'antd/dist/antd.less'
  4. import { BrowserRouter as Router } from "react-router-dom";
  5. import { main as mainConfig } from "./router/router";
  6. import { RenderRoutes } from "./router/allocation";
  7. function App () {
  8. return (
  9. <Router>
  10. <div className="App">
  11. <RenderRoutes routes={mainConfig}></RenderRoutes>
  12. </div>
  13. </Router>
  14. );
  15. }
  16. export default App;

5:接下来输入http://localhost:3000/loginhttp://localhost:3000就能看到首页和登录页了 

6:接着对index布局,采用antD的layout组件
index.js代码:

 

  1. import React, { Component } from "react";
  2. import { Layout, Switch, Menu } from "antd";
  3. // import { RenderRoutes } from "../../router/utils";
  4. import { Link } from "react-router-dom";
  5. // require("./index.less");
  6. const { Header, Content, Sider } = Layout;
  7. const { SubMenu } = Menu;
  8. class Index extends Component {
  9. state = {
  10. collapsed: false,
  11. theme: "dark",
  12. };
  13. onCollapse = (collapsed) => {
  14. console.log(collapsed);
  15. this.setState({ collapsed });
  16. };
  17. changeTheme = (value) => {
  18. this.setState({
  19. theme: value ? "dark" : "light",
  20. });
  21. };
  22. render() {
  23. const { collapsed } = this.state;
  24. let { routes } = this.props;
  25. console.log(routes);
  26. return (
  27. <Layout style={{ minHeight: "100vh" }}>
  28. <Sider
  29. theme={this.state.theme}
  30. collapsible
  31. collapsed={collapsed}
  32. onCollapse={this.onCollapse}
  33. >
  34. <div className="logo" />
  35. <Menu
  36. theme={this.state.theme}
  37. defaultSelectedKeys={["/user/experience"]}
  38. defaultOpenKeys={["sub1"]}
  39. mode="inline"
  40. >
  41. <Menu.Item key="1">
  42. <Link to="/user/WorkBench">工作台</Link>
  43. </Menu.Item>
  44. <SubMenu key="sub1" title="用户管理">
  45. <Menu.Item key="/user/experience">
  46. <Link to="/user/experience">体验用户</Link>
  47. </Menu.Item>
  48. <Menu.Item key="4">VIP用户</Menu.Item>
  49. <Menu.Item key="5">
  50. <Link to="/user/Details">用户详情</Link>
  51. </Menu.Item>
  52. </SubMenu>
  53. </Menu>
  54. </Sider>
  55. <Layout className="site-layout">
  56. <Header className="ant-layout-header" style={{ background: "#fff" }}>
  57. </Header>
  58. <Content style={{ margin: "16px 16px" }}>
  59. {/* <RenderRoutes routes={routes}></RenderRoutes> */}
  60. </Content>
  61. </Layout>
  62. </Layout>
  63. );
  64. }
  65. }
  66. export default Index;

接下来输http://localhost:3000就能看到首页了

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

闽ICP备14008679号