溫馨提示×

溫馨提示×

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

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

升級react-router4遇到的問題有哪些

發布時間:2021-08-30 11:27:30 來源:億速云 閱讀:186 作者:小新 欄目:web開發

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

react-router,V4版本修改內容

1. 所有組件更改為從react-router-dom導入

之前的所有路由組件均是從react-router中導入,在我之前的項目中,導入相關組件如下:

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

在react-router4 開始,所有的router組件均是從react-router-dom中導入, 所以一下的需要更改為以下代碼:

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

細心的你發現在,到導入的過程中,我刪除了Router,但是增加了BorwerRouter和Switch,下面我會一步步的說明.

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>

現在需要更改為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>

細心的你發現,這里的代碼不僅僅是將Router替換為BrowserRouter,而且還把所有的Route中用Switch包裹起來. 下面就是v4的另一個修改.

3. <BrowserRouter>只能有一個子節點

<BroserRouter>只能有一個子節點,所以官網建議的是使用<Switch>進行包裹,官網給出的例子如下.

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. 最坑的地方:在當前目錄下的文件路徑不再使用./, 而是直接用/.

在進行文件引用的時候 ,./src/js的寫法需要更改文'/src/js', 這是更改之后最坑的地方!!! 因為其他的更改,在控制臺都會有著詳細的錯誤提示, 然而找不到文件只會出現404!!!
所以,在單頁面中的引入的css文件和bundle.js的引入都需要更改, 在我的項目中的例子如下:

//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>

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

//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>

看完了這篇文章,相信你對“升級react-router4遇到的問題有哪些”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女