react-router4 嵌套路由的使用方法
更新時間:2017年07月24日 15:03:45 作者:isykw
本篇文章主要介紹了react-router4 嵌套路由的使用方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
react我自己還在摸索學(xué)習(xí)中,今天正好學(xué)習(xí)一下react-router4 嵌套路由的使用方法,順便留著筆記
先直接貼代碼
import React from 'react';
import ReactDOM from 'react-dom';
import { HashRouter as Router, Route, Switch} from 'react-router-dom';
import createBrowserHistory from 'history/createBrowserHistory';
import UserAddPage from './pages/UserAdd/index';
import HomePage from './pages/Home/index';
import HomeLayout from './components/HomeLayout/index';
const hashHistory = createBrowserHistory();
const NoMatch = ({ location }) => (
<div>
<h3>無法匹配 <code>{location.pathname}</code></h3>
</div>
)
ReactDOM.render((
<Router history={hashHistory}>
<div>
<HomeLayout>//總會加載這個組件,并且下面 swicth 里面的組件會在它里面 render
<Switch>
<Route path="/" exact component={HomePage}/>
<Route path="/user/add" component={UserAddPage}/>
<Route component={NoMatch}/>
</Switch>
</HomeLayout>
</div>
</Router>
), document.getElementById('root'));
參考文章:https://stackoverflow.com/questions/42095600/nested-routes-in-v4
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解React開發(fā)中使用require.ensure()按需加載ES6組件
本篇文章主要介紹了詳解React開發(fā)中使用require.ensure()按需加載ES6組件,非常具有實用價值,需要的朋友可以參考下2017-05-05
React Native之ListView實現(xiàn)九宮格效果的示例
本篇文章主要介紹了React Native之ListView實現(xiàn)九宮格效果的示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08

