<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當(dāng)前位置: 首頁 - 科技 - 知識(shí)百科 - 正文

        React-router v4 路由配置方法小結(jié)

        來源:懂視網(wǎng) 責(zé)編:小采 時(shí)間:2020-11-27 22:33:00
        文檔

        React-router v4 路由配置方法小結(jié)

        React-router v4 路由配置方法小結(jié):本文主要介紹了React-router v4 路由配置方法小結(jié),分享給大家,也給自己留個(gè)筆記 一. Switch 、Router 、Route三者的區(qū)別 1、Route Route 是建立location 和 ui的最直接聯(lián)系 2、Router react-router v4 中,Router被拆分成了S
        推薦度:
        導(dǎo)讀React-router v4 路由配置方法小結(jié):本文主要介紹了React-router v4 路由配置方法小結(jié),分享給大家,也給自己留個(gè)筆記 一. Switch 、Router 、Route三者的區(qū)別 1、Route Route 是建立location 和 ui的最直接聯(lián)系 2、Router react-router v4 中,Router被拆分成了S

        本文主要介紹了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

        文檔

        React-router v4 路由配置方法小結(jié)

        React-router v4 路由配置方法小結(jié):本文主要介紹了React-router v4 路由配置方法小結(jié),分享給大家,也給自己留個(gè)筆記 一. Switch 、Router 、Route三者的區(qū)別 1、Route Route 是建立location 和 ui的最直接聯(lián)系 2、Router react-router v4 中,Router被拆分成了S
        推薦度:
        標(biāo)簽: 路由 路由配置 總結(jié)
        • 熱門焦點(diǎn)

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 亚洲高清专区日韩精品| 亚洲成年看片在线观看| 精品无码一区二区三区亚洲桃色 | 97无码免费人妻超级碰碰碰碰 | 巨胸狂喷奶水视频www网站免费| 大胆亚洲人体视频| 一级做a爰片性色毛片免费网站| 亚洲高清成人一区二区三区| 四虎国产精品成人免费久久 | 亚洲色婷婷综合开心网| 中文字幕在线观看免费| 亚洲av永久无码精品漫画| 久久一本岛在免费线观看2020| 7777久久亚洲中文字幕蜜桃| 很黄很黄的网站免费的| 亚洲色欲啪啪久久WWW综合网| 国产国产成年年人免费看片| 大片免费观看92在线视频线视频| 亚洲乳大丰满中文字幕| 100部毛片免费全部播放完整| 亚洲女女女同性video| 国产亚洲情侣一区二区无| 99爱免费观看视频在线| 亚洲精品9999久久久久无码| 亚洲高清无码在线观看| 182tv免费视视频线路一二三| 亚洲AV色无码乱码在线观看| 亚洲人成色7777在线观看| 天天影院成人免费观看| 免费在线观看自拍性爱视频| 亚洲av无码成人黄网站在线观看| 五月婷婷综合免费| 免费无码午夜福利片| 日本久久久久亚洲中字幕| 在线观看免费精品国产| 免费一区二区无码东京热| 7777久久亚洲中文字幕| 亚洲日韩欧洲无码av夜夜摸| 青娱乐免费在线视频| 久草免费福利在线| 亚洲色偷偷综合亚洲AV伊人蜜桃|