请求数据时给请求头加上token值(全局加)

时间:2021-03-15 10:48:06   收藏:0   阅读:0
在main.js中加上以下代码
// 添加请求拦截器
axios.interceptors.request.use(config => {
  // 在发送请求之前做些什么
  //判断是否存在token,如果存在将每个页面header都添加token
  //if中的值为登陆时存入vuex中的token值
//如果存在token值,将token放入请求头‘Authorization’中
  if(store.state.token.token){
  config.headers.common[‘Authorization‘]=store.state.token.token
  }
    
  return config;
  }, error => {
  // 对请求错误做些什么
  return Promise.reject(error);
  });
 

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!