我目前正在努力使用react路由器v4来嵌套路由。
最接近的示例是React-Router v4 Documentation中的路由配置。
我想把我的应用分成两个不同的部分。
一个前端和一个管理区。
我在想这样的事情:
<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} />
前端的布局和风格与管理区域不同。因此,在首页中,回家的路线,大约,所以一个应该是子路线。
/home应该呈现到Frontpage组件中,/admin/home应该呈现在后端组件中。
我尝试了一些变体,但总是以没有点击/home或/admin/home而告终。
编辑- 19.04.2017
因为这篇文章现在有很多观点,所以我用最终的解决方案更新了它。我希望它能帮助一些人。
编辑- 08.05.2017
删除了旧的解决方案
最终解决方案:
这是我现在使用的最终解决方案。这个示例还有一个全局错误组件,类似于传统的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;
发布于 2017-04-10 03:35:43
在react- <Routes />
**. -v4中不嵌套相反,您将它们放入另一个** <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>
)
这是直接来自react-router documentation的basic example。
发布于 2017-03-02 22:12:01
我只想提一下react-路由器v4彻底改变了,因为这个问题被张贴/回答了。
不再有<Match>
组件了!<Switch>
是为了确保只渲染第一个匹配项。<Redirect>
well ..重定向到另一个路由。使用或不使用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>
</Router>
希望对你有帮助,让我知道。如果这个例子没有很好地回答你的问题,请告诉我,我会看看我是否可以修改它。
发布于 2019-08-22 10:30:26
我成功地通过Switch
包装定义了嵌套路由,并在定义嵌套路由之前定义了根路由。
<BrowserRouter>
<Switch>
<Route path="/staffs/:id/edit" component={StaffEdit} />
<Route path="/staffs/:id" component={StaffShow} />
<Route path="/staffs" component={StaffIndex} />
</Switch>
</BrowserRouter>
参考:https://github.com/ReactTraining/react-router/blob/master/packages/react-router/docs/api/Switch.md
https://stackoverflow.com/questions/41474134
复制相似问题