如何在react-redux中编写拦截器? [英] How to write interceptor in react-redux?
本文介绍了如何在react-redux中编写拦截器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
登录每个api后,我必须传递从服务器收到的令牌.所以我的问题是如何编写一个拦截器,该拦截器将在每个api调用上附加令牌.我正在使用请求承诺"来调用api.
I have to pass token, received from server, after login to every api. so my question is how to write an interceptor which will append token on every api call. I am using 'request-promise' to call api.
谢谢!
推荐答案
我将为您的请求承诺"调用创建包装器(接口)函数:
I would create a wrapper (interface) function for your 'request-promise' calls:
export default class RequestInterface {
constructor(accessToken) {
this.accessToken = accessToken;
}
request({
method,
uri,
body,
}) {
return rp({
method,
uri,
body,
qs: {
access_token: this.accessToken,
},
});
}
}
这样,每次进行RequestInterface.request()
呼叫时,您都将在那里拥有凭据.
This way on every RequestInterface.request()
call, you'll have your credentials there.
(我没有对此进行测试,但这是基本思想)
(I didn't test this but that's the basic idea)
这篇关于如何在react-redux中编写拦截器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文