禁用反应路由器 2 中的后退按钮 [英] disable back button in react-router 2

查看:50
本文介绍了禁用反应路由器 2 中的后退按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 react-router 2.我的路由定义为

I am using react-router 2. My routes are defined as

   <Route path="/" component={App}>
       <IndexRoute component={Home}/>
       <Route path="/about" component={About}/>
       <Route path="/login" component={Login} onEnter={redirectToDashboard}/>
       <Route path="/logout" component={Logout} onEnter={logoutSession}/>
       <Route path="/dashboard" component={Dashboard} onEnter={redirectToLogin}/>
   </Route>

一切正常,但我无法从仪表板页面禁用后退按钮.

Everything working fine but I am having problem disabling back button from my dashboard page.

成功登录后,我将用户重定向到仪表板页面,但是当用户单击后退按钮时,它会再次进入登录页面.当用户在仪表板页面上时,我想禁用 browser 的后退按钮.

After successful login I am redirecting user to dashboard page but when user clicks back button it goes to login page again. I want to disable back button of browser when user is on dashboard page.

推荐答案

最好的办法是,当用户登录时,他/她会被重定向到 dashbaord.如果由于某种原因用户点击后退按钮,您应该:

Your best bet, is when the user is login he/ she is redirected to dashbaord. if for some reason the user click on back button you should:

如果用户已登录留在页面仪表板

if the user is logged in stay on the page dashboard

if(logged) {
  history.pushState(null, null, location.href);
  window.onpopstate = function(event) {
    history.go(1);
  };
}

回不去了.

这篇关于禁用反应路由器 2 中的后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆