国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

模塊化react-router配置方法詳解

2024-05-06 15:38:31
字體:
來源:轉載
供稿:網友

react-router模塊化配置

因為公司的需要最近踏進了react坑,一直在挖坑填坑,在路由這一塊折騰得不行。

直接進入主題,配置react-router模塊化

1.先下載react-router-dom

npm install react-router-dom --save

2.在相應的文件引入react-router-dom相應的模塊

import { BrowserRouter as Router, Route, Link } from "react-router-dom";

3.在src子創建一個module目錄,接著在module目錄在創建一個router.js文件,用來配置路由。

//router.jsimport Index from '../components/Index'import New from '../components/New'  import NewList from '../components/NewList'  import NewContent from '../components/NewContent'  const routes = [    {    path:"/",    component:Index,    exact:true  },  {    path:"/new",    component:New,    routes:[      {        path:"/new/",        component:NewContent      },      {        path:"/new/newList",        component:NewList      }    ]  },  ]export default routes

4.在app.js根目錄添加相應的跳轉路徑。

//app.jsimport React from 'react';import './App.css';import { BrowserRouter as Router, Route, Link } from "react-router-dom";import router from "./modules/routers"function App() { return (  <Router>      <nav className="nav">        <ul>          <li>            <Link to="/">首頁</Link>          </li>          <li>            <Link to="/new">新聞</Link>          </li>        </ul>      </nav>      {        router.map((router,index)=>{                      if(router.exact){                            return <Route exact key={index} path={router.path}                render = {                  props =>(                    <router.component {...props} routes = {router.routes}/>                  )                }              />                          }else{                            return <Route key={index} path={router.path}                render = {                  props =>(                    <router.component {...props} routes = {router.routes} />                  )                }              />                          }                  })      }    </Router> );}export default App;

注意點:嵌套路由千萬不要在<Route/>身上加上component={xxx.xxx},否則在子路由頁碼就接受不到父路由傳遞給子路由的數據,重要的事情說三篇

注意點:嵌套路由千萬不要在<Route/>身上加上component={xxx.xxx},否則在子路由頁碼就接受不到父路由傳遞給子路由的數據,重要的事情說三篇

注意點:嵌套路由千萬不要在<Route/>身上加上component={xxx.xxx},否則在子路由頁碼就接受不到父路由傳遞給子路由的數據,重要的事情說三篇

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 禄劝| 芒康县| 错那县| 邓州市| 苍梧县| 丰台区| 密云县| 阳春市| 龙海市| 阳信县| 兴城市| 梁河县| 舟曲县| 手机| 周宁县| 瓦房店市| 西藏| 色达县| 雅安市| 连江县| 鹤壁市| 泊头市| 噶尔县| 邹平县| 澄江县| 徐汇区| 布拖县| 信阳市| 黄冈市| 福海县| 辽源市| 舒兰市| 陈巴尔虎旗| 定日县| 博爱县| 洛南县| 宿迁市| 玉溪市| 南郑县| 偏关县| 涡阳县|