【发布时间】:2017-08-29 20:20:39
【问题描述】:
我想在收到 403 错误时在 axios 拦截器中进行重定向。但是如何访问 React 组件之外的历史记录?
在Navigating Programatically in React-Router v4 中,它在 React 组件的上下文中,但这里我在 axios 上下文中尝试
axios.interceptors.response.use(function (response) {
// Do something with response data
return response;
}, function (error) {
// Do something with response error
if(error.response.status === 403) { console.log("Redirection needed !"); }
// Trow errr again (may be need for some other catch)
return Promise.reject(error);
});
【问题讨论】:
-
试试这个代码:
import {browserHistory} from 'react-router';然后browserHistory.push("/path"); -
用
import {browserHistory} from 'react-router-dom';和browserHistory.push("/path")尝试过,但它不起作用,这是V3 方式,不是吗? -
是的,不幸的是,这似乎不适用于 V4 路由器...
-
react-router-v6 stackoverflow.com/q/69953377/14665310问题的链接
标签: reactjs react-router axios