react我自己還在摸索學習中,今天正好學習一下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
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com