vue -- 请求拦截器

mac2022-06-30  29

所谓的拦截器,其实可以理解为请求拦截,意义就是在发送请求或者响应请求之前做一些我们需要判断的事情,比如发送登录请求时判断token是否过期,是否需要携带token值,都可以在请求之前配置

import axios from 'axios' // 配置默认的host,假如你的API host是:http://api.htmlx.club axios.defaults.baseURL = 'http://api.htmlx.club' // 添加请求拦截器 axios.interceptors.request.use(function (config) {   // 在发送请求之前做些什么   return config }, function (error) {   // 对请求错误做些什么 return Promise.reject(error) }); // 添加响应拦截器 axios.interceptors.response.use(function (response) {   // 对响应数据做点什么   return response }, function (error) {   // 对响应错误做点什么   return Promise.reject(error) });

转载于:https://www.cnblogs.com/zjh-study/p/10647368.html

最新回复(0)