react react-router-config


路由我已经安装过了,执行下面命令

npm install react-router-config -S

 

目录结构

 

 

 

index.js

 

 

 

Home.js

import { renderRoutes } from "react-router-config";
import { routes } from "../../router/router"; // 导入路由配置文件
 
在用的地方,这个是根路由
{renderRoutes(routes)} 
 
 
router.js
 
用哪个就引入哪个,我这个只是写了几个
 
import Edit from "../pages/product/edit/Edit";
import AddProduct from "../pages/product/addProduct/addProduct";
import Lol from "../components/lol/Lol";
 
const routes = [
  {
    path: "/",
    exact: true, // 不加的话就都是 / 路径
    component: Main,
  },
  {
    path: "/edit",
    component: Edit,
  },
  {
    path: "/addProduct",
    component: AddProduct,
    routes: [
      {
        path: "/addProduct/lol",
        component: Lol,
      },
      {
        path: "/addProduct/gok",
        component: Gok, // 无状态组件
      },
    ]
  }
];
export { routes };
 
在addProduct.js里面使用,这个页面里面还有两个子路由
 

 

 

子路由页面 Lol.js
 

 

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM