使用React Router V4/V5的嵌套路由 [英] Nested routes with react router v4 / v5
问题描述
我目前正在使用React Router 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.
编辑-2017年4月19日
因为此帖子现在有很多意见,所以我用最终解决方案对其进行了更新.我希望它能对某人有所帮助.
Because this post has a lot of views right now I updated it with the final solution. I hope it helps someone.
编辑-2017年8月5日
删除了旧的解决方案
最终解决方案:
这是我现在正在使用的最终解决方案.此示例还具有全局错误组件,例如传统的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中,您不嵌套<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} />
使用
const Topics = ({ match }) => (
<div>
<h2>Topics</h2>
<Link to={`${match.url}/exampleTopicId`}>
Example topic
</Link>
<Route path={`${match.path}/:topicId`} component={Topic}/>
</div>
)
Here is a basic example straight from the react-router documentation.
这篇关于使用React Router V4/V5的嵌套路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!