首页 > 技术文章 > vue全局设置请求头 (封装axios请求)

wang-xx 2019-07-25 14:27 原文

Vue.http.interceptors.push((request, next) => { 

   // 请求发送前的处理逻辑

  request.headers.set('Authorization',"Bearer " + 想要增加的变量)

  next((response) => {

    // 请求发送后的处理逻辑

    // 根据请求的状态,response参数会返回给successCallback或errorCallback

    return response

  })

})

封装请求 也可以设置header

 

export const $post = (url, params) => {
  return axios({
    method: 'post',
    url: url,
    data: params,
    headers: {
      'token':store.state.token
    }
  });
};

export const $get = (url) => {
  return axios({
    method: 'get',
    url: url,
    headers: {
      'token':store.state.token
    }
  });
};

 

推荐阅读