模塊化react-router配置方法詳解
react-router模塊化配置
因?yàn)楣镜男枰罱みM(jìn)了react坑,一直在挖坑填坑,在路由這一塊折騰得不行。
直接進(jìn)入主題,配置react-router模塊化
1.先下載react-router-dom
npm install react-router-dom --save
2.在相應(yīng)的文件引入react-router-dom相應(yīng)的模塊
import { BrowserRouter as Router, Route, Link } from "react-router-dom";
3.在src子創(chuàng)建一個(gè)module目錄,接著在module目錄在創(chuàng)建一個(gè)router.js文件,用來配置路由。
//router.js
import 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根目錄添加相應(yīng)的跳轉(zhuǎn)路徑。
//app.js
import 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;
注意點(diǎn):嵌套路由千萬不要在<Route/>身上加上component={xxx.xxx},否則在子路由頁碼就接受不到父路由傳遞給子路由的數(shù)據(jù),重要的事情說三篇
注意點(diǎn):嵌套路由千萬不要在<Route/>身上加上component={xxx.xxx},否則在子路由頁碼就接受不到父路由傳遞給子路由的數(shù)據(jù),重要的事情說三篇
注意點(diǎn):嵌套路由千萬不要在<Route/>身上加上component={xxx.xxx},否則在子路由頁碼就接受不到父路由傳遞給子路由的數(shù)據(jù),重要的事情說三篇
解析一下,<Route/>里面的render,這是官方給出的一種固定寫法,為了解決父路由傳遞數(shù)據(jù)給子路由接受不到的問題。render是一個(gè)對(duì)象,里面是一個(gè)箭頭函數(shù),箭頭函數(shù)放回<router.component {...props} routes = {router.routes} />一個(gè)標(biāo)簽,router.component的router對(duì)于的是你map傳進(jìn)來的那個(gè)形參,傳啥寫啥;component 是配置文件對(duì)應(yīng)的component ,routes 是傳給子路由的數(shù)據(jù)、(子路由通過this.props.routes 接收)
5.在有子路由的頁碼配置跳轉(zhuǎn)
import React ,{Component} from 'react';
import { BrowserRouter as Router, Route, Link } from "react-router-dom";
class New extends Component{
render(){
return(
<div className="box">
<div className="left">
<ul>
<li>
<Link to="/new">New</Link>
</li>
<li>
<Link to="/new/newList">NewList</Link>
</li>
</ul>
</div>
<div className="right">
{
this.props.routes.map((item,index)=>{
return <Route key={index} exact path={item.path} component={item.component}></Route>
})
}
</div>
</div>
)
}
}
export default New
最后的結(jié)果為:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
useEffect中return函數(shù)的作用和執(zhí)行時(shí)機(jī)解讀
這篇文章主要介紹了useEffect中return函數(shù)的作用和執(zhí)行時(shí)機(jī),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
React中g(shù)etDefaultProps的使用小結(jié)
React中的getDefaultProps功能允許開發(fā)者為類組件定義默認(rèn)屬性,提高組件的靈活性和容錯(cuò)性,本文介紹了getDefaultProps的作用、語法以及最佳實(shí)踐,并探討了其他替代方案,如函數(shù)組件中的默認(rèn)參數(shù)、高階組件和ContextAPI等,理解這些概念有助于提升代碼的可維護(hù)性和用戶體驗(yàn)2024-09-09
React項(xiàng)目中使用zustand狀態(tài)管理的實(shí)現(xiàn)
zustand是一個(gè)用于狀態(tài)管理的小巧而強(qiáng)大的庫,本文主要介紹了React項(xiàng)目中使用zustand狀態(tài)管理的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10
React父子組件傳值(組件通信)的實(shí)現(xiàn)方法
本文主要介紹了React父子組件傳值(組件通信)的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
React Native中ScrollView組件輪播圖與ListView渲染列表組件用法實(shí)例分析
這篇文章主要介紹了React Native中ScrollView組件輪播圖與ListView渲染列表組件用法,結(jié)合實(shí)例形式詳細(xì)分析了ScrollView組件輪播圖與ListView渲染列表組件具體功能、使用方法與操作注意事項(xiàng),需要的朋友可以參考下2020-01-01
React錯(cuò)誤邊界Error Boundaries
錯(cuò)誤邊界是一種React組件,這種組件可以捕獲發(fā)生在其子組件樹任何位置的JavaScript錯(cuò)誤,并打印這些錯(cuò)誤,同時(shí)展示降級(jí)UI,而并不會(huì)渲染那些發(fā)生崩潰的子組件樹2023-01-01

