本文主要介紹了React-router v4 路由配置方法小結(jié),分享給大家,也給自己留個(gè)筆記
一. Switch 、Router 、Route三者的區(qū)別
1、Route
Route 是建立location 和 ui的最直接聯(lián)系
2、Router
react-router v4 中,Router被拆分成了StaticRouter、MemoryRouter、BrowserRouter、HashRouter、NativeRouter。
MemoryRouter、BrowserRouter、HashRouter 等于
import { Router } from 'react-router' <!--這里可以有三種--> <!--history 部分源碼 exports.createBrowserHistory = _createBrowserHistory3.default; exports.createHashHistory = _createHashHistory3.default; exports.createMemoryHistory = _createMemoryHistory3.default; --> import createBrowserHistory from 'history/createBrowserHistory' // const history = createBrowserHistory() <Router history={history}> <App/> </Router>
NativeRouter(給rn使用的)
A <Router> for iOS and Android apps built using React Native.
這里新增strict 和 exact
使用了strict location 大于等于path才能匹配,eq path='/one' location='/one/a'能匹配。
使用了exact location 約等于 path 才能匹配,eq path='/one' location='/one'或者 '/one/'能匹配,所以說是約等于。
使用了exact 和 strict location = path才能匹配
StaticRouter(后續(xù)補(bǔ)充)
3、Switch
這是v4版本中新添加,主要用來做唯一匹配的功能。就是想要在眾多路由中只匹配其中一個(gè)路由。
二、v4 版本中路由應(yīng)該如何配置呢?
1.基本配置(這個(gè)和v3中基本一致,效果也基本一樣)
匹配 <= location eq.( /b => / + /b ) ( / => / )
<BrowserRouter forceRefresh={!supportsHistory} keyLength={12}> <div> <Route path="/" component={aContainer} /> <Route path="/b" component={bContainer} /> </div> </BrowserRouter>
2.含Switch 配置
匹配 <= location eq.( /b => /b ) ( / => / ) 唯一匹配
<BrowserRouter forceRefresh={!supportsHistory} keyLength={12}> <Switch> //這里用exact,僅僅是擔(dān)心location被 path='/'截胡了。 <Route exact path="/" component={aContainer} /> <Route path="/b" component={bContainer} /> </Switch> </BrowserRouter>
問題(三個(gè)問題)
1.如何設(shè)置公共的Component
第一種方式
<BrowserRouter forceRefresh={!supportsHistory} keyLength={12}> <div> <Route path="/" component={aContainer} /> <Route path="/b" component={bContainer} /> </div> </BrowserRouter>
第二種方式(父子嵌套)
<BrowserRouter forceRefresh={!supportsHistory} keyLength={12}> <div > <Route path="/" component={aContainer} /> <Route path="/b" component={Parent} /> {/* {app()} */} </div> </BrowserRouter>
const Parent = ({ match }) => ( <div> <Route path={`${match.url}/`} component={bContainer} /> <Route path={`${match.url}/c`} component={cContainer} /> <Route path={`${match.url}/d`} component={dContainer} /> </div> );
這種情況 bContainer就是是公用的Component
2.如何設(shè)置getComponent,按需加載
另一篇文章
3.是否有簡化寫法
npm install --save react-router-config
第一步 配置路由
const routes = [ { component: bContainer, routes: [ { path: '/', exact: true, component: bContainer }, { path: '/b/b', component: bContainer, routes: [ { path: '/b/b/b', component: bContainer } ] } ] } ]
第二步 設(shè)置路由
<BrowserRouter forceRefresh={!supportsHistory} keyLength={12}> <div > {renderRoutes(routes)} </div> </BrowserRouter>
第三步 需要在container的render中去調(diào)用方法
<div> 1111 {renderRoutes(this.props.route.routes)} </div>
這個(gè)優(yōu)勢是可以統(tǒng)一配置,劣勢是需要在container中統(tǒng)一調(diào)用,但是這個(gè)抽出來統(tǒng)一實(shí)現(xiàn),問題也不大,并且還可以解決 問題一。
這個(gè)renderRoutes實(shí)際是就是用一層Switch和多個(gè)Route來包了一層。
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com