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