溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

React-router v4路由配置的示例分析

發(fā)布時間:2021-08-18 14:04:42 來源:億速云 閱讀:102 作者:小新 欄目:web開發(fā)

這篇文章主要介紹了React-router v4路由配置的示例分析,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

一. 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ù)補充)

3、Switch

這是v4版本中新添加,主要用來做唯一匹配的功能。就是想要在眾多路由中只匹配其中一個路由。

二、v4 版本中路由應(yīng)該如何配置呢?

1.基本配置(這個和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>

問題(三個問題)

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>

這個優(yōu)勢是可以統(tǒng)一配置,劣勢是需要在container中統(tǒng)一調(diào)用,但是這個抽出來統(tǒng)一實現(xiàn),問題也不大,并且還可以解決 問題一。

這個renderRoutes實際是就是用一層Switch和多個Route來包了一層。

React-router v4路由配置的示例分析

感謝你能夠認真閱讀完這篇文章,希望小編分享的“React-router v4路由配置的示例分析”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI