react嵌套路由
<h2>1、结构目录</h2><p><img src="https://img2020.cnblogs.com/blog/1187630/202004/1187630-20200416105449904-810580890.png" alt=""></p>
<h2>2、路由配置文件,参照的vue-router</h2>
<p>config.js</p>
<div class="cnblogs_code">
<pre>import Login from '../view/Login.js'<span style="color: rgba(0, 0, 0, 1)">;
import System from </span>'../view/System.js'<span style="color: rgba(0, 0, 0, 1)">;
import Bus from </span>'../view/Bus.js'<span style="color: rgba(0, 0, 0, 1)">;
import Bus22 from </span>'../view/Bus22.js'<span style="color: rgba(0, 0, 0, 1)">;
const routes </span>=<span style="color: rgba(0, 0, 0, 1)"> [
{
path: </span>"/"<span style="color: rgba(0, 0, 0, 1)">,
component: Login,
exact: </span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
},
{
path: </span>"/system"<span style="color: rgba(0, 0, 0, 1)">,
component: System,
auth:</span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
routes: [
{
path: </span>"/system/bus"<span style="color: rgba(0, 0, 0, 1)">,
component: Bus,
exact: </span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
auth:</span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
},
{
path: </span>"/system/bus22"<span style="color: rgba(0, 0, 0, 1)">,
component: Bus22,
exact: </span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
auth:</span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
},
]
}
];
export </span><span style="color: rgba(0, 0, 255, 1)">default</span> routes;</pre>
</div>
<h2>3、路由主体,使用react-router-dom,详细文档里面也有其它的例子和API</h2>
<div class="cnblogs_code">
<pre>import routes from './config'<span style="color: rgba(0, 0, 0, 1)">;
import React, {Component} from </span>'react'<span style="color: rgba(0, 0, 0, 1)">;
import {
BrowserRouter as Router,
Switch,
Route,
Redirect,
} from </span>"react-router-dom"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">一级路由</span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
class First extends Component {
render() {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
</span><Router>
<Switch><span style="color: rgba(0, 0, 0, 1)">
{routes.map((route, i) </span>=><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (route.auth) {<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">根据配置是否检测登录</span>
<span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
</span><PrivateRoute key={i} path={route.path}>
<route.component/>
</PrivateRoute>
<span style="color: rgba(0, 0, 0, 1)"> )
} </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 0, 255, 1)">return</span> (<RouteWithSubRoutes key={i} {...route} />)
<span style="color: rgba(0, 0, 0, 1)"> }
}
)}
</span></Switch>
</Router>
<span style="color: rgba(0, 0, 0, 1)"> );
}
}
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">二级路由</span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
class SystemRouter extends Component {
render() {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
</span><Switch><span style="color: rgba(0, 0, 0, 1)">
{routes[</span>1].routes.map((route, i) =><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (route.auth) {<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">根据配置是否检测登录</span>
<span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
</span><PrivateRoutekey={i} path={route.path}>
<route.component/>
</PrivateRoute>
<span style="color: rgba(0, 0, 0, 1)"> )
} </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 0, 255, 1)">return</span> (<RouteWithSubRoutes key={i} {...route} />)
<span style="color: rgba(0, 0, 0, 1)"> }
})}
</span></Switch>
<span style="color: rgba(0, 0, 0, 1)"> );
}
}
export {
First, SystemRouter
};
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> A special wrapper for <Route> that knows how to</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)"> handle "sub"-routes by passing them in a `routes`</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)"> prop to the component it renders.</span><span style="color: rgba(0, 128, 0, 1)">
/*</span><span style="color: rgba(0, 128, 0, 1)">开放路由</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> RouteWithSubRoutes(route) {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
</span><<span style="color: rgba(0, 0, 0, 1)">Route
exact</span>=<span style="color: rgba(0, 0, 0, 1)">{route.exact}
path</span>=<span style="color: rgba(0, 0, 0, 1)">{route.path}
render</span>={props =><span style="color: rgba(0, 0, 0, 1)"> (
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> pass the sub-routes down to keep nesting</span>
<route.component {...props} routes={route.routes}/>
<span style="color: rgba(0, 0, 0, 1)"> )}
</span>/>
<span style="color: rgba(0, 0, 0, 1)">);
}
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">登录检测路由</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> PrivateRoute({children, ...rest}) {
let isAuthenticated </span>=<span style="color: rgba(0, 0, 0, 1)"> sessionStorage.auth;
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
</span><<span style="color: rgba(0, 0, 0, 1)">Route
{...rest}
render</span>={({location}) =><span style="color: rgba(0, 0, 0, 1)">
isAuthenticated </span>?<span style="color: rgba(0, 0, 0, 1)"> (
children
) : (
</span><<span style="color: rgba(0, 0, 0, 1)">Redirect
to</span>=<span style="color: rgba(0, 0, 0, 1)">{{
pathname: </span>"/"<span style="color: rgba(0, 0, 0, 1)">,
state: {from: location}
}}
</span>/>
<span style="color: rgba(0, 0, 0, 1)"> )
}
</span>/>
<span style="color: rgba(0, 0, 0, 1)">);
}</span></pre>
</div>
<h2>4、一级路由的使用,在App.js</h2>
<div class="cnblogs_code">
<pre>import React from 'react'<span style="color: rgba(0, 0, 0, 1)">;
import </span>'./App.css'<span style="color: rgba(0, 0, 0, 1)">;
import {First} from </span>'./router/index'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> App() {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
</span><First/>
<span style="color: rgba(0, 0, 0, 1)">);
}
export </span><span style="color: rgba(0, 0, 255, 1)">default</span> App;</pre>
</div>
<h2>5、二级路由的使用,在一级路由的组件文件中,本例是System.js</h2>
<div class="cnblogs_code">
<pre>import React,{Component} from 'react'<span style="color: rgba(0, 0, 0, 1)">;
import {SystemRouter} from </span>'../router/index'<span style="color: rgba(0, 0, 0, 1)">;
import {
withRouter
} from </span>"react-router-dom"<span style="color: rgba(0, 0, 0, 1)">;
import { Menu, Icon } from </span>'antd'<span style="color: rgba(0, 0, 0, 1)">;
const { SubMenu } </span>=<span style="color: rgba(0, 0, 0, 1)"> Menu;
class System extends Component{
componentDidMount() {
}
handleClick </span>= e =><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.props.history.push(e.key)
};
render() {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
</span><div>
<header><span style="color: rgba(0, 0, 0, 1)">
嵌套路由
</span></header>
<<span style="color: rgba(0, 0, 0, 1)">Menu
onClick</span>={<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.handleClick}
style</span>={{ width: 256,<span style="color: rgba(0, 0, 255, 1)">float</span>:'left'<span style="color: rgba(0, 0, 0, 1)"> }}
defaultSelectedKeys</span>={['1'<span style="color: rgba(0, 0, 0, 1)">]}
defaultOpenKeys</span>={['sub1'<span style="color: rgba(0, 0, 0, 1)">]}
mode</span>="inline"
>
<<span style="color: rgba(0, 0, 0, 1)">SubMenu
key</span>="sub1"<span style="color: rgba(0, 0, 0, 1)">
title</span>=<span style="color: rgba(0, 0, 0, 1)">{
</span><span>
<Icon type="mail" />
<span>Navigation One</span>
</span>
<span style="color: rgba(0, 0, 0, 1)"> }
</span>>
<Menu.Item key="/system/bus">bus</Menu.Item>
<Menu.Item key="/system/bus22">bus22</Menu.Item>
</SubMenu>
</Menu>
<SystemRouterstyle={{ <span style="color: rgba(0, 0, 255, 1)">float</span>:'left' }}/>
</div>
<span style="color: rgba(0, 0, 0, 1)"> );
}
}
export </span><span style="color: rgba(0, 0, 255, 1)">default</span> withRouter(System);</pre>
</div>
<p>最后来俩效果图,页面分三部分,头部、左侧导航、右侧内容,切换时只有右侧内容变化。</p>
<p><img src="https://img2020.cnblogs.com/blog/1187630/202004/1187630-20200416111146138-1164787857.png" alt=""></p>
<p> </p>
<p><img src="https://img2020.cnblogs.com/blog/1187630/202004/1187630-20200416111332378-427132102.png" alt=""></p>
<p> </p>
<p> </p>
<p><img src="https://img2020.cnblogs.com/blog/1187630/202004/1187630-20200416111347177-1087516413.png" alt=""></p>
<p> </p><br><br>
来源:https://www.cnblogs.com/gxp69/p/12711412.html
頁:
[1]