国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > JS > 正文

react router 4.0以上的路由應(yīng)用詳解

2024-05-06 16:39:54
字體:
供稿:網(wǎng)友

本文介紹了react router 4.0以上的路由應(yīng)用,分享給大家,具體如下:

在4.0以下的react router中,嵌套的路由可以放在一個(gè)router標(biāo)簽中,形式如下,嵌套的路由也直接放在一起。

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

但是在4.0以后,嵌套的路由與之前的就完全不同了,需要單獨(dú)放置在嵌套的根component中去處理路由,否則會(huì)一直有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>  <h2>Topics</h2>  <Route path={`${match.url}/:userId`} component={Profile}/> </div>) 

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

一個(gè)完整的嵌套路由的例子如下

說明及注意事項(xiàng)

1.以下代碼采用ES6格式

2.react-router-dom版本為4.1.1

3.請注意使用諸如HashRouter之類的history,否則一直會(huì)有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>    <h1>App</h1>    <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>  <h3>About</h3> </div>)const Home = () => ( <div>  <h3>Home</h3> </div>)const Message = ({ match }) => ( <div>  <h3>new messages</h3>  <h3>{match.params.id}</h3> </div>)const Inbox = ({ match }) => ( <div>  <h2>Topics</h2>  <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'));

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VeVb武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請移步到JavaScript/Ajax教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 宁晋县| 恩施市| 治多县| 内丘县| 缙云县| 汽车| 汝城县| 柯坪县| 阿图什市| 个旧市| 香河县| 句容市| 分宜县| 太仓市| 罗田县| 吉木萨尔县| 德兴市| 安庆市| 湟中县| 福安市| 河间市| 西贡区| 时尚| 綦江县| 兴安县| 庐江县| 富源县| 巴林右旗| 瑞丽市| 黔东| 安国市| 湟源县| 宜都市| 天镇县| 定兴县| 西林县| 阳城县| 漠河县| 昌邑市| 兴山县| 和林格尔县|