Javascript React router goBack()工作不正常

Javascript React router goBack()工作不正常,javascript,reactjs,meteor,react-router,react-router-dom,Javascript,Reactjs,Meteor,React Router,React Router Dom,我遇到了一个问题。我习惯于路由。它运转良好,但戈巴克运转不正常。当我点击后退按钮时,它首先进入未找到/登录页面,然后重定向到后退页面。我如何克服这个问题 import React from 'react'; import { Router, Route, Switch } from 'react-router-dom'; import createBrowserHistory from 'history/createBrowserHistory'; import Signin from '..

我遇到了一个问题。我习惯于路由。它运转良好,但戈巴克运转不正常。当我点击后退按钮时,它首先进入
未找到
/
登录
页面,然后重定向到后退页面。我如何克服这个问题

import React from 'react';
import { Router, Route, Switch } from 'react-router-dom';
import createBrowserHistory from 'history/createBrowserHistory';

import Signin from '../ui/signin/Signin';
import AddEvent from '../ui/events/AddEvent';
import EventView from '../ui/events/EventView';
import NotFound from '../ui/NotFound';

const history = createBrowserHistory();
const privatePages = [
    '/events', 
    '/addevent',

];
const publicPages = ['/', '/signup','/forgotpassword'];

const onEnterPublicPage = () => {
    if (Meteor.userId()) {
        history.replace('/events');
    }
};

const onEnterPrivatePage = () => {
    if (!Meteor.userId()) {
        history.replace('/');
    }
};

export const onAuthenticationChange = (isAuthenticated) => {

    const pathname = this.location.pathname;
    const isUnauthenticatedPage = publicPages.includes(pathname);
    const isAuthenticatedPage = privatePages.includes(pathname);

    if (isAuthenticated && isUnauthenticatedPage) {
        history.replace('/events');
    } else if (!isAuthenticated && isAuthenticatedPage) {
        history.replace('/');
    }
}

export const routes = (
    <Router history = {history}>
        <Switch>
            <Route 
            exact path="/events" 
            component={ListEvents} 
            onEnter={onEnterPrivatePage} />

            <Route 
            exact path="/addevent" 
            component={AddEvent} 
            onEnter={onEnterPrivatePage} />

            <Route  component={NotFound}/>

            <Route 
            exact path="/" 
            component={Signin} 
            onEnter={onEnterPublicPage} />

        </Switch>
    </Router>
);
在返回中

<Link
    to="" 
    onClick={this.goBack}
    className="back-icon">
    Back
</Link>

返回

这是因为您正在使用
历史记录。请替换('/')
。您正在替换,而不是推送,因此没有以前的路由。

一种可能的方法是使用history.push动态更改路由,而不是使用Link。要实现这一点,请删除链接组件并在“li”或“button”上定义onClick事件。现在,首先执行onClick函数中的所有任务,最后使用history.push更改路线意味着在其他页面上导航


我希望这有帮助

我是否替换了
历史。替换('/')
历史。推('/')
?您可以尝试使用
历史。推('/')
,然后使用
goBack
我使用过类似于“goBack(){this.props.history.goBack();}”的goBack。是吗?对不起,它坏了。我添加了组件代码。请看这个。您可以查看的文档。另外,请检查
<Link
    to="" 
    onClick={this.goBack}
    className="back-icon">
    Back
</Link>