亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

vue循環(huán)中調(diào)用接口-promise.all();按順序執(zhí)行異步處理方式

 更新時(shí)間:2023年07月19日 16:08:35   作者:儲(chǔ)儲(chǔ)隨記  
這篇文章主要介紹了vue循環(huán)中調(diào)用接口-promise.all();按順序執(zhí)行異步處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

vue循環(huán)調(diào)用接口-promise.all();按順序執(zhí)行異步處理

在vue中循環(huán)調(diào)用接口-promise.all()

methods: {
? handleAdd (arr) {
? ? ?this.loading = true
? ? ?const allApi = []
? ? ?arr.forEach((item, index) => {
? ? ? ?const data = {
? ? ? ? ?id: item.id,
? ? ? ? ?name: item.name
? ? ? ?}
? ? ? ?const oneApi = api.add(data).then(res => {
? ? ? ? ?if (res.error_code === 0) {
? ? ? ? ? ?this.$message.success(res.msg)
? ? ? ? ?} else {
? ? ? ? ? ?this.$message.error(res.msg)
? ? ? ? ?}
? ? ? ?})
? ? ? ?allApi.push(oneApi)
? ? ?})
? ? ?Promise.all(allApi).then(() => {
? ? ? ?this.loading = false
? ? ?})
? ?}
}

有異步處理數(shù)據(jù)時(shí),按順序執(zhí)行函數(shù)

methods: {
?? ?handleAdd (val) {
? ? ? ? this.addTag(val).then(() => {
? ? ? ? ? this.tags.push(this.newTag)
? ? ? ? ? this.editNote()
? ? ? ? })
? ? },
?? ?addTag (val) {
? ? ? const data = {
? ? ? ? tag: val
? ? ? }
? ? ? return add(data).then(res => {
? ? ? ? this.newTag = {
? ? ? ? ? id: res.data.id,
? ? ? ? ? name: res.data.name
? ? ? ? }
? ? ? })
? ? },
? ? editNote () {
? ? ? const data = {
? ? ? ? tags: this.tags,
? ? ? }
? ? ? update(data).then((res) => {
? ? ? ? if (res.error_code === 0) {
? ? ? ? ? this.$message.success('修改成功!')
? ? ? ? ?}
? ? ? })
? ? }
}

使用return返回第一個(gè)異步處理的結(jié)果;使用then,繼續(xù)執(zhí)行第二個(gè)異步處理(當(dāng)?shù)谝淮畏祷亟Y(jié)果為ture時(shí))。

vue中Promise.all的使用

Promise.all

簡(jiǎn)述

Promise.all可以將多個(gè)Promise實(shí)例包裝成一個(gè)新的Promise實(shí)例。同時(shí),成功和失敗的返回值是不同的,成功的時(shí)候返回的是一個(gè)結(jié)果數(shù)組,而失敗的時(shí)候則返回最先被reject失敗狀態(tài)的值。

Promse.all在處理多個(gè)異步處理時(shí)非常有用,比如說一個(gè)頁(yè)面上需要等兩個(gè)或多個(gè)ajax的數(shù)據(jù)回來以后才正常顯示,在此之前只顯示loading圖標(biāo)。

注意: Promise.all成功結(jié)果數(shù)組里的數(shù)據(jù)順序和Promise.all接收到的數(shù)組順序是一致的。

所以在前端開發(fā)請(qǐng)求數(shù)據(jù)的過程中,偶爾會(huì)遇到發(fā)送多個(gè)請(qǐng)求并根據(jù)請(qǐng)求順序獲取和使用數(shù)據(jù)的場(chǎng)景,使用Promise.all毫無疑問可以解決這個(gè)問題。

舉例

let P1 = new Promise((resolve, reject) => {
  resolve('成功')
})
let P2 = new Promise((resolve, reject) => {
  resolve('success')
})
let P3 = Promse.reject('失敗')
Promise.all([P1, P2]).then((result) => {
  console.log(result)     //控制臺(tái)打印結(jié)果['成功了', 'success']
}).catch((error) => {
  console.log(error)
})
Promise.all([P1,P3,P2]).then((result) => {
  console.log(result)
}).catch((error) => {
  console.log(error)      // 控制臺(tái)打印結(jié)果 '失敗'
})

實(shí)戰(zhàn)

這里實(shí)現(xiàn)的功能是調(diào)用后臺(tái)接口返回?cái)?shù)據(jù)實(shí)現(xiàn)全選反選

在這里插入圖片描述

<template>
  <div class="table-container-wapper" id="apps-permission" v-loading="isTableLoading">
    <div class="func-btn">
      <el-button @click="selectInvert" class="invert">反選</el-button>
      <span class="cur">/</span>
      <el-button @click="selectAll" class="allSelect">全選</el-button>
    </div>
    <div class="choose">
      <div v-for="(item, index) in form" :key="index" class="select-list">
        <el-checkbox  v-model="item.select">{{ item.serviceName }}</el-checkbox>
      </div>
    </div>
    <div class="foot">
      <el-button class="cancel" size="small" @click="$router.back()">取 消</el-button>
      <el-button type="success" size="small" @click="submit">確 定</el-button>
    </div>
  </div>
</template>
<script>
import BaseMixin from "@/mixins/base";
import request from "@/utils/request";
import SETTING from "@/settings";
export default {
  mixins: [BaseMixin],
  data() {
    return {
      clientId: this.$route.query.id,
      form: [],
    };
  },
  created() {
    this.isTableLoading = true
    Promise.all([
      this.getServiceInfo(),
      this.getList()
    ]).then(([form, data]) => {
      let hasArr = data.map(item => item.serviceId)
      form.forEach(item => {
        if(hasArr.includes(item.id)) {
          item.select = true
        }else {
          item.select = false
        }
      })
      this.form = form
      this.isTableLoading = false
    }, _ => { 
      this.isTableLoading = false
    })
  },
  methods: {
    getServiceInfo() {
      return new Promise((resolve, reject) => {
        request({
          url: `${SETTING.IOT_APPLICATION}/serviceInfo`,
          params: {
            page: this.pagination.page - 1,
            size: 1000,
          },
        }).then(
          (res) => {
            if (res.code=== "200") {
              resolve(res.data.content)
            }
            reject()
          },
          (_) => {
            reject()
            this.$message({
              type: "error",
              message: _.message || "查詢列表失敗",
            });
          }
        );
      })  
    },
    getList() {
      return new Promise((resolve, reject) => {
        request({
          url: `${SETTING.IOT_APPLICATION}/sdkAppServiceRelation/curRights/${this.clientId}`,
        }).then(
          (res) => {
            if (res[SETTING.IOT_THING_MODEL_STATES] === "200") {
              resolve(res.data)
            }
            reject()
          },
          (_) => {
            reject()
            this.$message({
              type: "error",
              message: _.message || "查詢列表失敗",
            });
          }
        );
      })
    },
    //全選
    selectAll() {
      console.log(111)
      this.form.forEach((item) => {
        item.select = true;
      });
    },
    //反選
    selectInvert() {
      this.form.forEach((item) => {
        item.select = !item.select;
      });
    },
    //提交
    submit() {
      let ids = this.form.filter(item => item.select).map(item => item.id)
      request({
        url: `${SETTING.IOT_APPLICATION}/sdkAppServiceRelation/rights`,
        method: "post",
        data: {
          clientId: this.clientId,
          ids: ids
        }
      }).then(
        (res) => {
          if (res[SETTING.IOT_THING_MODEL_STATES] === "200") {
            this.$message({
              type: "success",
              message: res.msg || res.message ||  "操作成功",
            });
            this.$router.back()
          }
        },
        (_) => {
          reject()
          this.$message({
            type: "error",
            message: _.message || "查詢列表失敗",
          });
        }
      );
    },
  },
};
</script>
<style lang="scss" scope>
#apps-permission {
  max-width: 1000px;
  .func-btn {
    overflow: hidden;
    margin-top: 10px;
    .invert {
      border: 0px;
      padding: 0;
      float: right;
      font-size: 16px;
    }
    .cur {
      margin-left: 5px;
      margin-right: 5px;
      float: right;
      font-size: 16px;
    }
    .allSelect {
      border: 0px;
      padding: 0;
      float: right;
      font-size: 16px;
    }
  }
  .choose {
    display: flex;
    flex-wrap: wrap;
    .select-list{
      margin-bottom: 12px;
      width: 25%;
    }
  }
  .foot {
    display: flex;
    justify-content: flex-end;
    margin-top: 20px;
  }
}
</style>

擴(kuò)展知識(shí):Promise.race,哪個(gè)結(jié)果快就返回哪個(gè)

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Vue3 AST解析器-源碼解析

    Vue3 AST解析器-源碼解析

    這篇文章我們從 ast 生成時(shí)調(diào)用的 baseParse 函數(shù)分析,再到 baseParse 返回 createRoot 的調(diào)用結(jié)果,一直到細(xì)化的講解了 parseChildren 解析子節(jié)點(diǎn)函數(shù)中的其中某一個(gè)具體解析器的執(zhí)行過程。最后通過一個(gè)簡(jiǎn)單模板舉例,需要的朋友可以參考下
    2021-09-09
  • vue+electron實(shí)現(xiàn)創(chuàng)建多窗口及窗口間的通信(實(shí)施方案)

    vue+electron實(shí)現(xiàn)創(chuàng)建多窗口及窗口間的通信(實(shí)施方案)

    這篇文章主要介紹了vue+electron實(shí)現(xiàn)創(chuàng)建多窗口及窗口間的通信,本文給大家分享實(shí)施方案結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-09-09
  • Vue 全家桶實(shí)現(xiàn)移動(dòng)端酷狗音樂功能

    Vue 全家桶實(shí)現(xiàn)移動(dòng)端酷狗音樂功能

    這篇文章主要介紹了Vue 全家桶實(shí)現(xiàn)移動(dòng)端酷狗音樂功能,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-11-11
  • 詳解vue的數(shù)據(jù)劫持以及操作數(shù)組的坑

    詳解vue的數(shù)據(jù)劫持以及操作數(shù)組的坑

    這篇文章主要介紹了vue的數(shù)據(jù)劫持以及操作數(shù)組的坑,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • vue實(shí)現(xiàn)把頁(yè)面導(dǎo)出成word文件的方法

    vue實(shí)現(xiàn)把頁(yè)面導(dǎo)出成word文件的方法

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)把頁(yè)面導(dǎo)出成word文件的方法,文中的實(shí)現(xiàn)步驟講解詳細(xì),并且有詳細(xì)的代碼示例,需要的小伙伴可以參考一下
    2023-10-10
  • vue項(xiàng)目中axios的封裝請(qǐng)求

    vue項(xiàng)目中axios的封裝請(qǐng)求

    這篇文章主要介紹了vue項(xiàng)目中axios的封裝請(qǐng)求,axios?是一個(gè)輕量的HTTP客戶端,它基于?XMLHttpRequest?服務(wù)來執(zhí)行?HTTP?請(qǐng)求,支持豐富的配置,下文更多詳細(xì)資料,需要的朋友可以參考一下
    2022-03-03
  • vue如何實(shí)現(xiàn)上傳圖片和顯示圖片

    vue如何實(shí)現(xiàn)上傳圖片和顯示圖片

    這篇文章主要介紹了vue如何實(shí)現(xiàn)上傳圖片和顯示圖片問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • vue通信方式EventBus的實(shí)現(xiàn)代碼詳解

    vue通信方式EventBus的實(shí)現(xiàn)代碼詳解

    這篇文章主要介紹了vue通信方法EventBus的實(shí)現(xiàn)代碼,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-06-06
  • vue 實(shí)現(xiàn)全選全不選的示例代碼

    vue 實(shí)現(xiàn)全選全不選的示例代碼

    本篇文章主要介紹了vue 實(shí)現(xiàn)全選全不選的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-03-03
  • vue-cli3 熱更新配置操作

    vue-cli3 熱更新配置操作

    這篇文章主要介紹了vue-cli3 熱更新配置操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09

最新評(píng)論