源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

详解升级react-router 4 踩坑指南

  • 时间:2022-11-18 05:07 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:详解升级react-router 4 踩坑指南
[b]一.前言[/b] 上午把近日用React做的一个新闻项目所依赖的包升级到了最新的版本,其中从react-router(2.8.1)升级到react-router(4.1.2)中出现了很多问题, 故总结一下在升级过程中遇到的问题. [b]二.react-router,V4版本修改内容[/b] [b]1. 所有组件更改为从react-router-dom导入[/b] 之前的所有路由组件均是从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,下面我会一步步的说明. [b]2. 将所有<Router>替换为<BrowserRouter>[/b] 之前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的另一个修改. [b]3. <BrowserRouter>只能有一个子节点[/b] <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>
)
[b]4. 最坑的地方:在当前目录下的文件路径不再使用./, 而是直接用/.[/b] 在进行文件引用的时候 ,./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>
[b]三.更多信息[/b] 以上就是我在我的项目中所遇到的坑,以及对应的处理办法.总的来说react-router4 rewrite之后变化还是挺大的. 1. 更多React-router v4的修改信息,请看Github: [url=https://github.com/ReactTraining/react-router/blob/master/packages/react-router/docs/guides/migrating.md]Migrating from v2/v3 to v4 [/url] 2. 本文中的项目下载地址: Github: [url=https://github.com/Lee-Tanghui/React-news-project.git]https://github.com/Lee-Tanghui/React-news-project.git[/url] 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程素材网。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部