篮羽公爵 發表於 2020-4-16 11:15:00

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>&lt;Router&gt;
      &lt;Switch&gt;<span style="color: rgba(0, 0, 0, 1)">
          {routes.map((route, i) </span>=&gt;<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>&lt;PrivateRoute key={i} path={route.path}&gt;
                  &lt;route.component/&gt;
                  &lt;/PrivateRoute&gt;
<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> (&lt;RouteWithSubRoutes key={i} {...route} /&gt;)
<span style="color: rgba(0, 0, 0, 1)">            }
            }
          )}
      </span>&lt;/Switch&gt;
      &lt;/Router&gt;
<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>&lt;Switch&gt;<span style="color: rgba(0, 0, 0, 1)">
          {routes[</span>1].routes.map((route, i) =&gt;<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>&lt;PrivateRoutekey={i} path={route.path}&gt;
                  &lt;route.component/&gt;
                &lt;/PrivateRoute&gt;
<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> (&lt;RouteWithSubRoutes key={i} {...route} /&gt;)
<span style="color: rgba(0, 0, 0, 1)">            }
          })}
      </span>&lt;/Switch&gt;
<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 &lt;Route&gt; 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>&lt;<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 =&gt;<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>
      &lt;route.component {...props} routes={route.routes}/&gt;
<span style="color: rgba(0, 0, 0, 1)">      )}
    </span>/&gt;
<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>&lt;<span style="color: rgba(0, 0, 0, 1)">Route
      {...rest}
      render</span>={({location}) =&gt;<span style="color: rgba(0, 0, 0, 1)">
      isAuthenticated </span>?<span style="color: rgba(0, 0, 0, 1)"> (
          children
      ) : (
          </span>&lt;<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>/&gt;
<span style="color: rgba(0, 0, 0, 1)">      )
      }
    </span>/&gt;
<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>&lt;First/&gt;
<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 =&gt;<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>&lt;div&gt;
      &lt;header&gt;<span style="color: rgba(0, 0, 0, 1)">
          嵌套路由
      </span>&lt;/header&gt;
      &lt;<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"
      &gt;
          &lt;<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>&lt;span&gt;
            &lt;Icon type="mail" /&gt;
            &lt;span&gt;Navigation One&lt;/span&gt;
            &lt;/span&gt;
<span style="color: rgba(0, 0, 0, 1)">            }
          </span>&gt;
            &lt;Menu.Item key="/system/bus"&gt;bus&lt;/Menu.Item&gt;
            &lt;Menu.Item key="/system/bus22"&gt;bus22&lt;/Menu.Item&gt;
          &lt;/SubMenu&gt;
      
      &lt;/Menu&gt;
      &lt;SystemRouterstyle={{ <span style="color: rgba(0, 0, 255, 1)">float</span>:'left' }}/&gt;
      &lt;/div&gt;
<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>&nbsp;</p>
<p><img src="https://img2020.cnblogs.com/blog/1187630/202004/1187630-20200416111332378-427132102.png" alt=""></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><img src="https://img2020.cnblogs.com/blog/1187630/202004/1187630-20200416111347177-1087516413.png" alt=""></p>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/gxp69/p/12711412.html
頁: [1]
查看完整版本: react嵌套路由