升級(jí)react-router4遇到的問(wèn)題有哪些-創(chuàng)新互聯(lián)

小編給大家分享一下升級(jí)react-router4遇到的問(wèn)題有哪些,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

阜南網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),阜南網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為阜南成百上千提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)公司要多少錢(qián),請(qǐng)找那個(gè)售后服務(wù)好的阜南做網(wǎng)站的公司定做!

react-router,V4版本修改內(nèi)容

1. 所有組件更改為從react-router-dom導(dǎo)入

之前的所有路由組件均是從react-router中導(dǎo)入,在我之前的項(xiàng)目中,導(dǎo)入相關(guān)組件如下:

//v2
import {Router,Route,hashHistory} from 'react-router';

在react-router4 開(kāi)始,所有的router組件均是從react-router-dom中導(dǎo)入, 所以一下的需要更改為以下代碼:

//v4
import {Route,BrowserRouter, Switch} from 'react-router-dom';

細(xì)心的你發(fā)現(xiàn)在,到導(dǎo)入的過(guò)程中,我刪除了Router,但是增加了BorwerRouter和Switch,下面我會(huì)一步步的說(shuō)明.

2. 將所有<Router>替換為<BrowserRouter>

之前v2中的代碼如下:

//v2
 <Router history={hashHistory}>
  <Route path="/" component={PCIndex}></Route>
  <Route path="/details/:uniqueky" component={PCNewsDetails}></Route>
  <Route path="/usercenter" component={PCUserCenter}></Route>
 </Router>

現(xiàn)在需要更改為BrowserRouter

//v4
<BrowserRouter>
  <Switch>
   <Route exact path="/" component={MobileIndex}></Route>
   <Route path="/details/:uniqueky" component={MobileNewsDetails}></Route>
   <Route path="/usercenter" component={MobileUserCenter}></Route>
  </Switch>
 </BrowserRouter>

細(xì)心的你發(fā)現(xiàn),這里的代碼不僅僅是將Router替換為BrowserRouter,而且還把所有的Route中用Switch包裹起來(lái). 下面就是v4的另一個(gè)修改.

3. <BrowserRouter>只能有一個(gè)子節(jié)點(diǎn)

<BroserRouter>只能有一個(gè)子節(jié)點(diǎn),所以官網(wǎng)建議的是使用<Switch>進(jìn)行包裹,官網(wǎng)給出的例子如下.

In v3, you could specify a number of child routes, and only the first one that matched would be rendered.

// v3
<Route path='/' component={App}>
 <IndexRoute component={Home} />
 <Route path='about' component={About} />
 <Route path='contact' component={Contact} />
</Route>

v4 provides a similar functionality with the <Switch> component. When a <Switch> is rendered, it will only render the first child <Route> that matches the current location.

// v4
const App = () => (
 <Switch>
  <Route exact path='/' component={Home} />
  <Route path='/about' component={About} />
  <Route path='/contact' component={Contact} />
 </Switch>
)

4. 最坑的地方:在當(dāng)前目錄下的文件路徑不再使用./, 而是直接用/.

在進(jìn)行文件引用的時(shí)候 ,./src/js的寫(xiě)法需要更改文'/src/js', 這是更改之后最坑的地方!!! 因?yàn)槠渌母?在控制臺(tái)都會(huì)有著詳細(xì)的錯(cuò)誤提示, 然而找不到文件只會(huì)出現(xiàn)404!!!
所以,在單頁(yè)面中的引入的css文件和bundle.js的引入都需要更改, 在我的項(xiàng)目中的例子如下:

//v2
<!DOCTYPE html>
<html lang="en">
  <head>
    <title></title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="./src/css/pc.css" rel="external nofollow" >
    <link rel="stylesheet" href="./src/css/mobile.css" rel="external nofollow" >
  </head>
  <body>
    <div id="mainContainer">
      
    </div>
    <script src="./src/bundle.js"></script>
  </body>
</html>

上面的頁(yè)面需要更改為下面這樣,否則所有的文件都無(wú)法找到:

//v4
<!DOCTYPE html>
<html lang="en">
  <head>
    <title></title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="/src/css/pc.css" rel="external nofollow" >
    <link rel="stylesheet" href="/src/css/mobile.css" rel="external nofollow" >
  </head>
  <body>
    <div id="mainContainer">
      
    </div>
    <script src="/src/bundle.js"></script>
  </body>
</html>

看完了這篇文章,相信你對(duì)“升級(jí)react-router4遇到的問(wèn)題有哪些”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站制作公司行業(yè)資訊頻道,感謝各位的閱讀!

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線(xiàn),公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。

網(wǎng)頁(yè)標(biāo)題:升級(jí)react-router4遇到的問(wèn)題有哪些-創(chuàng)新互聯(lián)
文章出自:http://muchs.cn/article38/dsedsp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、全網(wǎng)營(yíng)銷(xiāo)推廣、微信公眾號(hào)用戶(hù)體驗(yàn)、App設(shè)計(jì)、面包屑導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作