I am currently struggling with nesting routes using react router v4.
(我目前正在使用react router v4进行嵌套路由。)
The closest example was the route config in the React-Router v4 Documentation .
(最接近的示例是React-Router v4文档中的路由配置。)
I want to split my app in 2 different parts.
(我想将我的应用程序拆分为两个不同的部分。)
A frontend and an admin area.
(前端和管理区域。)
I was thinking about something like this:
(我在考虑这样的事情:)
<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 should be rendered into the Frontpage component and /admin/home should be rendered within the Backend component.
(/ home应该呈现在Frontpage组件中,而/ admin / home应该在Backend组件中呈现。)
I tried some variations but I always ended in not hitting /home or /admin/home.
(我尝试了一些变化,但我总是在没有击中/ home或/ admin / home。)
Edit - 19.04.2017
(编辑 - 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.(我希望它对某人有帮助。)
Edit - 08.05.2017
(编辑 - 08.05.2017)
Removed old solutions
(删除旧解决方案)
Final solution:
(最终解决方案)
This is the final solution I am using right now.
(这是我现在使用的最终解决方案。)
This example also has a global error component like a traditional 404 page.(此示例还有一个全局错误组件,如传统的404页面。)
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;
ask by datoml translate from so