React中如何在ajax请求中做路由跳转

clipboard.png
如图,在axios请求response中对特定状态码做路由跳转,但是react-router v4已经移除了browserHistory,所以跳转失效,除了window.location.href跳转,react有没有比较友好的方式在非组件中跳转路由

阅读 5k
3 个回答
import { createBrowserHistory } from 'history';

const history = createBrowserHistory();
history.replace('/login');

如果事路由之内的组件,用this.props.history.push(''),不是路由之内的组件,从react-router-dom引入withRouter,用它包裹组件,就可以从props中拿到history。withRouter(Component)

  1. 使用connected-react-redux, 将store传入进去,直接

import { push } from 'connected-redux-router'

store.dispatch(push('/path'))
  1. 直接使用使用window.location = '/xxx'跳转
  2. 传入history 在创建Router对象传入的history对象
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题