我目前正在使用反应路由器v4在嵌套路线上苦苦挣扎。
I am currently struggling with nesting routes using react router v4.
最接近的例子是中的路由配置 React-Router v4文档。
我想将我的应用拆分为2不同的部分。
I want to split my app in 2 different parts.
前端和管理区域。
我在考虑这样的事情:
<Match pattern="/" component={Frontpage}> <Match pattern="/home" component={HomePage} /> <Match pattern="/about" component={AboutPage} /> </Match> <Match pattern="/admin" component={Backend}> <Match pattern="/home" component={Dashboard} /> <Match pattern="/users" component={UserPage} /> </Match> <Miss component={NotFoundPage} />前端的布局和样式与管理区域不同。所以在首页的路线回家,大约应该是儿童路线。
The frontend has a different layout and style than the admin area. So within the frontpage the route home, about and so one should be the child routes.
/ home 应该被渲染到Frontpage组件中 / admin / home 应在Backend组件中呈现。
/home should be rendered into the Frontpage component and /admin/home should be rendered within the Backend component.
我尝试了一些变化,但我总是没有点击/ home或/ admin / home。
I tried some variations but I always ended in not hitting /home or /admin/home.
编辑 - 19.04.2017
因为这篇文章现在有很多观点我用最终解决方案更新了它。我希望它有所帮助。
Because this post has a lot of views right now I updated it with the final solution. I hope it helps someone.
编辑 - 08.05.2017
删除旧版解决方案
最终解决方案:
这是我正在使用的最终解决方案现在。此示例还有一个全局错误组件,如传统的404页面。
This is the final solution I am using right now. This example also has a global error component like a traditional 404 page.
import React, { Component } from 'react'; import { Switch, Route, Redirect, Link } from 'react-router-dom'; const Home = () => <div><h1>Home</h1></div>; const User = () => <div><h1>User</h1></div>; const Error = () => <div><h1>Error</h1></div> const Frontend = props => { console.log('Frontend'); return ( <div> <h2>Frontend</h2> <p><Link to="/">Root</Link></p> <p><Link to="/user">User</Link></p> <p><Link to="/admin">Backend</Link></p> <p><Link to="/the-route-is-swiggity-swoute">Swiggity swooty</Link></p> <Switch> <Route exact path='/' component={Home}/> <Route path='/user' component={User}/> <Redirect to={{ state: { error: true } }} /> </Switch> <footer>Bottom</footer> </div> ); } const Backend = props => { console.log('Backend'); return ( <div> <h2>Backend</h2> <p><Link to="/admin">Root</Link></p> <p><Link to="/admin/user">User</Link></p> <p><Link to="/">Frontend</Link></p> <p><Link to="/admin/the-route-is-swiggity-swoute">Swiggity swooty</Link></p> <Switch> <Route exact path='/admin' component={Home}/> <Route path='/admin/user' component={User}/> <Redirect to={{ state: { error: true } }} /> </Switch> <footer>Bottom</footer> </div> ); } class GlobalErrorSwitch extends Component { previousLocation = this.props.location componentWillUpdate(nextProps) { const { location } = this.props; if (nextProps.history.action !== 'POP' && (!location.state || !location.state.error)) { this.previousLocation = this.props.location }; } render() { const { location } = this.props; const isError = !!( location.state && location.state.error && this.previousLocation !== location // not initial render ) return ( <div> { isError ? <Route component={Error} /> : <Switch location={isError ? this.previousLocation : location}> <Route path="/admin" component={Backend} /> <Route path="/" component={Frontend} /> </Switch>} </div> ) } } class App extends Component { render() { return <Route component={GlobalErrorSwitch} /> } } export default App;推荐答案
在react-router-v4中你不要't nest < Routes /> 。相反,你把它们放在另一个< Component /> 中。
In react-router-v4 you don't nest <Routes />. Instead, you put them inside another <Component />.
例如
<Route path='/topics' component={Topics}> <Route path='/topics/:topicId' component={Topic} /> </Route>应该变成
<Route path='/topics' component={Topics} />with
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 文档。
Here is a basic example straight from the react-router documentation.
更多推荐
具有反应路由器v4的嵌套路由
发布评论