我正在使用 React 和 React Router 构建单页应用程序。
每当我在一个页面上时,例如“/banners”,然后我转到“/banners/edit/1234”,一切都按预期工作。但是当我使用我的浏览器按钮返回到“/banners”时,我被重定向到“/”。当我使用 this.props.history.goBack(); 时也会发生同样的事情。它首先重定向到上一页,然后立即重定向到/。
如何让用户转到“/banner”或上一页?或者这对 React Router 来说是不可能的吗?或者有人建议去哪里看吗?
import React, { Component } from 'react';
import { Switch, Route, Redirect } from 'react-router-dom'
import { PropsRoute } from 'react-router-with-props';
import Grid from '@material-ui/core/Grid';
import Content from './content.js';
import Edit from './edit.js';
import NotFound from './notfound.js';
import New from './new.js';
class Main extends Component {
render() {
return(
<Grid container justify="center">
<main>
<Switch>
<Route exact path="/:type" render={(props) => <Content {...props} tabs={this.props.data} />} />
<Route exact path="/edit/:type/:id" component={Edit} />
<Route exact path="/:type/new" component={New} />
<Redirect exact from="/" to={"/"+this.props.data[0].toLowerCase()} />
<Route component={NotFound} />
</Switch>
</main>
</Grid>
)
}
}
export default Main
最佳答案
无论如何阅读这个问题。
好的,所以问题似乎不是 React-Router 的错。在我的代码中的某个地方,我用一组允许的路由检查了路由。由于某种原因,这错误地重定向了。这样就解释了重定向。
关于javascript - React Router 重定向到错误的页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50908527/