如何在使用全局拦截器的同时为特定请求单独配置响应处理?
如果在使用 axios 的全局拦截器时,希望为某个请求单独设置响应拦截,可以考虑以下方法:
添加自定义参数来控制是否显示异常提示。具体步骤如下:
// 模拟请求 export const login = (data) => { return request.post('/login', data, { showerrortips: true }); };
service.interceptors.response.use( (response) => { // ... 省略其他 // 从 response 的 config 属性中获取自定义参数 const { config } = response; if (config.showErrorTips) { // 给出提示 } }, (error) => { // ... 省略其他 } );