interface IDefaultLayoutProps { children: React.ReactNode } const DefaultLayout: React.SFC<IDefaultLayoutProps> = ({children}) => { return ( <div className="DefaultLayout"> {children} </div> ); } const LayoutRoute: React.SFC<IDefaultLayoutRouteProps & RouteProps> = ({component: Component, layout: Layout, ...rest}) => { const handleRender = (matchProps: RouteComponentProps<{}, StaticContext>) => ( <Layout> <Component {...matchProps} /> </Layout> ); return ( <Route {...rest} render={handleRender}/> ); } const ScreenRouter = () => ( <BrowserRouter> <div> <Link to="/">Home</Link> <Link to="/counter">Counter</Link> <Switch> <LayoutRoute path="/" exact={true} layout={DefaultLayout} component={HomeScreen} /> <LayoutRoute path="/counter" layout={DashboardLayout} component={CounterScreen} /> </Switch> </div> </BrowserRouter> );
在这里我已经在TSX中创建了一个示例,如果有人想要删除子路径路径中包装路由的前缀: https://stackoverflow.com/a/47891060/5517306
的 在react-router-v4中,您不会嵌套 <Routes /> 。相反,你把它们放在另一个里面 <Component /> 。 强>
<Routes />
<Component />
例如
<Route path='/topics' component={Topics}> <Route path='/topics/:topicId' component={Topic} /> </Route>
应该成为
<Route path='/topics' component={Topics} />
同
const Topics = ({ match }) => ( <div> <h2>Topics</h2> <Link to={`${match.url}/exampleTopicId`}> Example topic </Link> <Route path={`${match.url}/:topicId`} component={Topic}/> </div> )
这里有一个 基本的例子 直接来自react-router 文件 。
只是想提一下react-router 的 v4彻底改变了 强> 因为这个问题被发布/回答了。
没有 <Match> 组件了! <Switch> 是确保只渲染第一个匹配。 <Redirect> 好..重定向到另一条路线。使用或遗漏 exact 进入或排除部分匹配。
<Match>
<Switch>
<Redirect>
exact
查看文档。他们都是伟大的。 https://reacttraining.com/react-router/
这是一个我希望可以回答你的问题的例子。
<Router> <div> <Redirect exact from='/' to='/front'/> <Route path="/" render={() => { return ( <div> <h2>Home menu</h2> <Link to="/front">front</Link> <Link to="/back">back</Link> </div> ); }} /> <Route path="/front" render={() => { return ( <div> <h2>front menu</h2> <Link to="/front/help">help</Link> <Link to="/front/about">about</Link> </div> ); }} /> <Route exact path="/front/help" render={() => { return <h2>front help</h2>; }} /> <Route exact path="/front/about" render={() => { return <h2>front about</h2>; }} /> <Route path="/back" render={() => { return ( <div> <h2>back menu</h2> <Link to="/back/help">help</Link> <Link to="/back/about">about</Link> </div> ); }} /> <Route exact path="/back/help" render={() => { return <h2>back help</h2>; }} /> <Route exact path="/back/about" render={() => { return <h2>back about</h2>; }} /> </div>
希望它有所帮助,让我知道。如果这个例子没有足够好地回答你的问题,请告诉我,我会看看是否可以修改它。
你可以尝试类似的东西 Routes.js
import React, { Component } from 'react' import { BrowserRouter as Router, Route } from 'react-router-dom'; import FrontPage from './FrontPage'; import Dashboard from './Dashboard'; import AboutPage from './AboutPage'; import Backend from './Backend'; import Homepage from './Homepage'; import UserPage from './UserPage'; class Routes extends Component { render() { return ( <div> <Route exact path="/" component={FrontPage} /> <Route exact path="/home" component={Homepage} /> <Route exact path="/about" component={AboutPage} /> <Route exact path="/admin" component={Backend} /> <Route exact path="/admin/home" component={Dashboard} /> <Route exact path="/users" component={UserPage} /> </div> ) } } export default Routes
App.js
import React, { Component } from 'react'; import logo from './logo.svg'; import './App.css'; import { BrowserRouter as Router, Route } from 'react-router-dom' import Routes from './Routes'; class App extends Component { render() { return ( <div className="App"> <Router> <Routes/> </Router> </div> ); } } export default App;
我想你也可以从这里实现同样的目标。
确实,为了嵌套路由,您需要将它们放在Route的子组件中。
但是,如果您更喜欢更“内联”的语法而不是在组件之间打破路由,则可以使用 render 支持 <Route> 你想嵌入下,使用内联无状态功能组件,如下所示:
render
<Route>
<BrowserRouter> <Route exact path="/" component={Frontpage} /> <Route path="/home" component={HomePage} /> <Route path="/about" component={AboutPage} /> <Route path="/admin" render={({ match: { url } }) => ( <> <Route exact path={`${url}/`} component={Backend} /> <Route path={`${url}/home`} component={Dashboard} /> <Route path={`${url}/users`} component={UserPage} /> </> )} /> </BrowserRouter>
如果你对原因感兴趣 render 应该用来代替 component ,它是为了阻止内联组件在每个渲染上重新安装。 请参阅文档 了解更多细节。
component
另请注意,上面的示例使用 反应16片段 包装嵌套的路由,只是为了使它更清洁。在React 16之前,你可以使用一个容器 <div> 而是在这里。
<div>
有点像这样。
import React from 'react'; import { BrowserRouter as Router, Route, NavLink, Switch, Link } from 'react-router-dom'; import '../assets/styles/App.css'; const Home = () => <NormalNavLinks> <h1>HOME</h1> </NormalNavLinks>; const About = () => <NormalNavLinks> <h1>About</h1> </NormalNavLinks>; const Help = () => <NormalNavLinks> <h1>Help</h1> </NormalNavLinks>; const AdminHome = () => <AdminNavLinks> <h1>root</h1> </AdminNavLinks>; const AdminAbout = () => <AdminNavLinks> <h1>Admin about</h1> </AdminNavLinks>; const AdminHelp = () => <AdminNavLinks> <h1>Admin Help</h1> </AdminNavLinks>; const AdminNavLinks = (props) => ( <div> <h2>Admin Menu</h2> <NavLink exact to="/admin">Admin Home</NavLink> <NavLink to="/admin/help">Admin Help</NavLink> <NavLink to="/admin/about">Admin About</NavLink> <Link to="/">Home</Link> {props.children} </div> ); const NormalNavLinks = (props) => ( <div> <h2>Normal Menu</h2> <NavLink exact to="/">Home</NavLink> <NavLink to="/help">Help</NavLink> <NavLink to="/about">About</NavLink> <Link to="/admin">Admin</Link> {props.children} </div> ); const App = () => ( <Router> <div> <Switch> <Route exact path="/" component={Home}/> <Route path="/help" component={Help}/> <Route path="/about" component={About}/> <Route exact path="/admin" component={AdminHome}/> <Route path="/admin/help" component={AdminHelp}/> <Route path="/admin/about" component={AdminAbout}/> </Switch> </div> </Router> ); export default App;