1. 安装

  1. yarn add react-router-dom

2.router.js

  1. /*
  2. * @Author: zulezhe
  3. * @Date: 2021-12-23 09:15:06
  4. * @LastEditors: wangchaoxu
  5. * @LastEditTime: 2021-12-23 09:15:53
  6. * @Path: https://gitee.com/zulezhe/
  7. * @Description:
  8. */
  9. import Login from "../pages/login/Login";
  10. import Home from "../pages/home/home";
  11. import User from "../pages/user";
  12. import UserTwo from "../pages/usertow";
  13. import Demo1 from "../pages/routerDemo/demo1";
  14. import Demo2 from "../pages/routerDemo/demo2";
  15. import Demo3 from "../pages/routerDemo/demo3";
  16. interface router {
  17. path: string;
  18. component: any;
  19. exact?: boolean;
  20. children?: Array<router>;
  21. }
  22. const routers: Array<router> = [
  23. {
  24. path: "/",
  25. exact: true,
  26. component: Login,
  27. },
  28. {
  29. path: "/home",
  30. component: Home,
  31. children: [
  32. {
  33. path: "/",
  34. component: Demo1,
  35. },
  36. {
  37. path: "/home/demo2",
  38. component: Demo2,
  39. },
  40. {
  41. path: "/home/demo3",
  42. component: Demo3,
  43. },
  44. ],
  45. },
  46. {
  47. path: "/home",
  48. component: Home,
  49. },
  50. {
  51. path: "/user",
  52. component: User,
  53. },
  54. {
  55. path: "/user/:userId",
  56. component: UserTwo,
  57. },
  58. ];
  59. export default routers;