美文网首页
axios拦截器2

axios拦截器2

作者: 前端划水工 | 来源:发表于2018-07-26 18:25 被阅读0次

有关axios另一种配置,给请求的接口加上时间

import axios from 'axios'
import qs from 'qs'
const install = function (Vue, config = {}) {
  // $axios
  Vue.prototype.$axios = (function () {
    var axiosDeploy = axios.create({
      headers: {
        'Cache-Control': 'no-cache',
        'Content-Type': 'application/x-www-form-urlencoded',
        'X-Requested-With': 'XMLHttpRequest'
      }
    })
    axiosDeploy.interceptors.request.use(function (config) {
      config.transformRequest = [
        function (data, header) {
          return qs.stringify(data)
        }
      ]
      if (config.params) {
        config.params._time = +new Date()
      } else {
        config.params = {
          _time: +new Date()
        }
      }
      return config
    })
    return axiosDeploy
  })()
export default {
  install
}

相关文章

网友评论

      本文标题:axios拦截器2

      本文链接:https://www.haomeiwen.com/subject/gmximftx.html