vue中axios.create创建实例 并使用axios.all并发请求

东明兄 2018-05-28
4条评论 21,792 次浏览
东明兄 2018-05-284条评论 21,792 次浏览

刚接触axios没多久,前面只是使用axios做一些基础的请求,今天做东西要在使用axios.create的情况下使用axios.all并发请求,尝试了几次后找到方法,这样写:

main.js中:

import axios from 'axios'

Vue.prototype.$axiosTools=axios;//将使用$axiosTools 来使用axios的all方法和spread方法
let http = axios.create({
  baseURL: 'http://xxxxx.com/index.php/Api/',
  timeout: 1000,
});
Vue.prototype.$axios=http;//这个$axios用来发请求

xxx.vue中

   created(){
    
      this.$axiosTools.all([this.getBanner(), this.getDesign()])
        .then(this.$axiosTools.spread(function (a, b) {
          // 两个请求现在都执行完成
          console.log(b);
          console.log(a);
        }));

    },
    methods: {

      getBanner() {
        return this.$axios.get('Api/getBanner');
      },
      getDesign() {
        return this.$axios.get('Index/design');
      }
    }

vue中axios.create创建实例 并使用axios.all并发请求” 有 4 条评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注