reactrouter4.0以上路由應(yīng)用的示例分析

小編給大家分享一下react router 4.0以上路由應(yīng)用的示例分析,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

成都創(chuàng)新互聯(lián)公司主要從事成都網(wǎng)站制作、成都網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)欽南,十載網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):13518219792

在4.0以下的react router中,嵌套的路由可以放在一個router標簽中,形式如下,嵌套的路由也直接放在一起。

<Route component={App}>
  <Route path="groups" components={Groups} />
  <Route path="users" components={Users}>
   <Route path="users/:userId" component={Profile} />
  </Route>
</Route>

但是在4.0以后,嵌套的路由與之前的就完全不同了,需要單獨放置在嵌套的根component中去處理路由,否則會一直有warning:

You should not use <Route component> and <Route children> in the same route

正確形式如下

<Route component={App}>
  <Route path="groups" components={Groups} />
  <Route path="users" components={Users}>
   //<Route path="users/:userId" component={Profile} />
  </Route>
</Route>

上面將嵌套的路由注釋掉

const Users = ({ match }) => (
 <div>
  <h3>Topics</h3>
  <Route path={`${match.url}/:userId`} component={Profile}/>
 </div>
)

上面在需要嵌套路由的component中添加新的路由

一個完整的嵌套路由的例子如下

說明及注意事項

1.以下代碼采用ES6格式

2.react-router-dom版本為4.1.1

3.請注意使用諸如HashRouter之類的history,否則一直會有warning,不能渲染

import React, { Component } from 'react';
import ReactDOM from 'react-dom';
// import { Router, Route, Link, Switch } from 'react-router';
import {
 HashRouter,
 Route,
 Link,
 Switch
} from 'react-router-dom';

class App extends Component {
 render() {
  return (
   <div>
    <h2>App</h2>
    <ul>
     <li><Link to="/">Home</Link></li>
     <li><Link to="/about">About</Link></li>
     <li><Link to="/inbox">Inbox</Link></li>
    </ul>
    {this.props.children}

   </div>
  );
 }
}

const About = () => (
 <div>
  <h4>About</h4>
 </div>
)

const Home = () => (
 <div>
  <h4>Home</h4>
 </div>
)

const Message = ({ match }) => (
 <div>
  <h4>new messages</h4>
  <h4>{match.params.id}</h4>
 </div>
)

const Inbox = ({ match }) => (
 <div>
  <h3>Topics</h3>
  <Route path={`${match.url}/messages/:id`} component={Message}/>

 </div>
) 

ReactDOM.render(
 (<HashRouter>
  <App>
    <Route exact path="/" component={Home} />
    <Route path="/about" component={About} />
    <Route path="/inbox" component={Inbox} />
  </App>
 </HashRouter>),
 document.getElementById('root')
);

看完了這篇文章,相信你對“react router 4.0以上路由應(yīng)用的示例分析”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

網(wǎng)站欄目:reactrouter4.0以上路由應(yīng)用的示例分析
當前地址:http://bm7419.com/article40/geggho.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動態(tài)網(wǎng)站關(guān)鍵詞優(yōu)化、營銷型網(wǎng)站建設(shè)、網(wǎng)站改版搜索引擎優(yōu)化、網(wǎng)站建設(shè)

廣告

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

成都app開發(fā)公司