import React, { Component } from 'react'; import { BrowserRouter as Router, Route, Link, Switch, Redirect } from 'react-router-dom' function renderRouter(routers) { return routers.map((route, index) => { if (route.children) { return ( <Route component={route.component} path={route.path}> {renderRouter(route.children)} </Route> ); } return ( < Route path={route.path} key={index} component={route.component} exact={route.exact} ></Route > ) }) } export default renderRouter;
使用
function App(props) { return ( <div className="App"> <Router> <Switch> <Route path='/login' render={(props) => { if (isAuthLogin()) { return <Redirect to={`/`} /> } else { return <Login {...props} /> } }}></Route> <Route path='/' render={(props) => { if (!isAuthLogin()) { return ( <Redirect to={`/login`} /> ) } return ( <Layout> <Switch> <Auth route={routers}></Auth> <Route path="*" component={NotFound} /> </Switch> </Layout> ) }} /> </Switch> </Router> </div> ); }
发表评论
侧栏公告
寄语
譬如朝露博客是一个分享前端知识的网站,联系方式11523518。
热评文章
标签列表
热门文章
友情链接