欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

axios 请求

程序员文章站 2022-07-02 18:46:17
...

普通,显示“正在加载”

  request() {
    let data = {
      id: "1000001"
    };
    console.log(data);
    this.$vux.loading.show({ text: "正在加载" });
    this.$api.indexInterface.requestData(data)
      .then(res => {
        this.$vux.loading.hide();
        res = res.data;
        if (res.success) {
          this.list = res.data;
        } else {
          this.$vux.toast.show({ type: "cancel", text: "加载失败" });  //res.retMsg
        }
      })
      .catch(err => {
        this.$vux.loading.hide();
        console.error(err);
      });
  },
复制代码
this.$api.indexInterface.requestData({
  id: "1000001"
}).then(res => {
  res = res.data;
  if (res.success) {
    this.table.count = res.data.count;
    this.table.body = (() => {
      let arr = [];
      [...res.data.dbPageList].map(m => arr.push(m));
      return arr;
    })();
  } else {
    this.$vux.toast.show({ type: "cancel", text: "加载失败" });  //res.retMsg
  }
}).catch(err => console.error(err))
复制代码

Promise封装

const promise = new Promise((resolve, reject) => {
  this.$api.indexInterface.requestData({
      id: "1000001"
  }).then(res => {
    res = res.data;
    res.success ? resolve(res.data) : reject(res.retMsg);
  }).catch(err => console.error(err))
});

promise.then(resolve => {
  [...resolve].map(m => this.table.body.push(m));
}, reject => {
  console.error(reject);
})
复制代码

复制代码