Reactjs React路由器&x2B;Axios拦截器。如何进行重定向?
我有一个axios拦截器,当用户被迫注销(因为令牌过期)时,我想返回我的主页 但我不知道该如何传递给它。我正在使用mobx,但不确定这是否能帮助我解决这个问题Reactjs React路由器&x2B;Axios拦截器。如何进行重定向?,reactjs,react-router,axios,react-router-v4,mobx,Reactjs,React Router,Axios,React Router V4,Mobx,我有一个axios拦截器,当用户被迫注销(因为令牌过期)时,我想返回我的主页 但我不知道该如何传递给它。我正在使用mobx,但不确定这是否能帮助我解决这个问题 export const axiosInstance = axios.create({ baseURL: 'https://localhost:44391/api', timeout: 5000, contentType: "application/json", Authorization: getAuth
export const axiosInstance = axios.create({
baseURL: 'https://localhost:44391/api',
timeout: 5000,
contentType: "application/json",
Authorization: getAuthToken()
})
axiosInstance.interceptors.response.use(function (response) {
return response;
}, function (error) {
const originalRequest = error.config;
if(error.code != "ECONNABORTED" && error.response.status === 401 && !originalRequest._retry){
originalRequest._retry = true;
return axiosInstance.post("/tokens/auth",{
"refreshToken": getRefreshToken(),
"grantType": "refresh_token"
}).then(response => {
localStorage.authentication = JSON.stringify(response.data);
updateAuthInstant();
return axiosInstance(originalRequest)
});
}
return Promise.reject(error);
});
您应该添加npm包。有了它,您可以创建浏览器历史记录,并在应用程序的其他位置使用它
例如:
// routing.js
import createHistory from 'history/createBrowserHistory';
export const history = createHistory();
在包含路由的组件中
import { Route, Router } from 'react-router-dom';
import { history } from './routing.js';
import ComponentA from './ComponentA';
import ComponentB from './ComponentB';
const Routes = () => (
<Router history={history}>
<div>
<Route exact path='/route1' component={ComponentA} />
<Route exact path='/route2' component={ComponentB} />
</div>
</Router>
);
调用changeRoute()
时,路径将更新为/route2
,并将呈现组件B
。您应该添加npm包。有了它,您可以创建浏览器历史记录,并在应用程序的其他位置使用它
例如:
// routing.js
import createHistory from 'history/createBrowserHistory';
export const history = createHistory();
在包含路由的组件中
import { Route, Router } from 'react-router-dom';
import { history } from './routing.js';
import ComponentA from './ComponentA';
import ComponentB from './ComponentB';
const Routes = () => (
<Router history={history}>
<div>
<Route exact path='/route1' component={ComponentA} />
<Route exact path='/route2' component={ComponentB} />
</div>
</Router>
);
调用changeRoute()
时,路径将更新为/route2
,并将呈现组件b
。window.location.href='/user/login'
window.location.href='/user/login'
在呈现顶级组件之前,是否需要进行任何可能重定向的ajax调用?我不确定您的意思,最常见的情况是发送请求、用户访问令牌过期、尝试使用刷新令牌。如果失败,请将其返回登录页面。在呈现顶级组件之前,是否需要进行任何可能重定向的ajax调用?我不确定您的意思,最常见的情况是发送请求,用户访问令牌过期,尝试使用刷新令牌。如果失败了,把他们踢回登录页面。啊,我正在使用createBrowserHistory,但我从来没有想过把它放在自己的文件中。使用它来填充我的存储,比如const browserHistory=createBrowserHistory();const history=syncHistoryWithStore(浏览器历史记录、路由存储);不过,我想问一个简单的问题。{history}&{browserHistory}browserHistory
是react路由器2和3的一部分。在react路由器4中,这被组合为。这是用于API使用的基本历史记录。如果你自己创建browserHistory,你可以这样做:-)。我应该切换到createHistory()而不是createBrowserHistory(),我想我不明白你的意思。你是指routing.js示例中我的导入方式吗?在该示例中,我将从history
包导入的createBrowserHistory
函数命名为createHistory
。这可以被命名为你想要的任何东西。啊,对了,我以为这是一个真正相似的函数名,但我猜这是导出的默认函数?啊,我正在使用createBrowserHistory,但我从来没有想过像这样将它放在它自己的文件中。使用它来填充我的存储,比如const browserHistory=createBrowserHistory();const history=syncHistoryWithStore(浏览器历史记录、路由存储);不过,我想问一个简单的问题。{history}&{browserHistory}browserHistory
是react路由器2和3的一部分。在react路由器4中,这被组合为。这是用于API使用的基本历史记录。如果你自己创建browserHistory,你可以这样做:-)。我应该切换到createHistory()而不是createBrowserHistory(),我想我不明白你的意思。你是指routing.js示例中我的导入方式吗?在该示例中,我将从history
包导入的createBrowserHistory
函数命名为createHistory
。这可以被命名为任何你想要的名称。啊,对了,我认为这实际上是一个类似的函数名,但我猜这是导出的默认函数?这将重新加载完整的应用程序,而不是最佳方法这将重新加载完整的应用程序,而不是最佳方法