【问题标题】:how to do history.push location inside axios interceptor with react react-router v4?如何使用 react react-router v4 在 axios 拦截器中进行 history.push 定位?
【发布时间】:2018-12-01 10:09:55
【问题描述】:

需要根据拦截器内部的状态推送到特定位置,例如“/login”。

有没有办法直接推送到某个位置?

【问题讨论】:

  • 请尝试使用this.props.history.push("/login");
  • @Jin 拦截器中的this.props 是什么?
  • 我正在尝试将组件推到外部。在拦截器内。

标签: reactjs push router react-router-v4 history


【解决方案1】:

一种快速而丑陋的方式可能是。 window.location.replace('/myUrl')

更新

解决方法可能是

/**RootComponent*/

export const myRouter = {};
@withRouter()
Class RootComponent extends React.Component {
    constructor(props) {
       super(props);
       myRouter = this.props.router;
    }
    render() {
      return <Provider store={store}>
            <MainComponent/>
         </Provider>
    }
}

/**拦截器*/

import { myRouter } from 'RootComponent';

if (status == 401) {
    myRouter.push('/myUrl');
}

【讨论】:

    猜你喜欢
    • 2017-08-29
    • 2021-12-25
    • 1970-01-01
    • 2018-06-08
    • 2018-01-21
    • 2018-07-08
    • 2018-12-25
    • 2018-11-27
    • 1970-01-01
    相关资源
    最近更新 更多