最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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í)非常有用,比如說(shuō)一個(gè)頁(yè)面上需要等兩個(gè)或多個(gè)ajax的數(shù)據(jù)回來(lái)以后才正常顯示,在此之前只顯示loading圖標(biāo)。

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

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

舉例

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 || "查詢(xún)列表失敗",
            });
          }
        );
      })  
    },
    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 || "查詢(xún)列表失敗",
            });
          }
        );
      })
    },
    //全選
    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 || "查詢(xún)列表失敗",
          });
        }
      );
    },
  },
};
</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)文章

  • vue發(fā)送ajax請(qǐng)求詳解

    vue發(fā)送ajax請(qǐng)求詳解

    如何利用vue進(jìn)行AJAX,其它vue本身不支持發(fā)送AJAX請(qǐng)求,需要使用vue-resource(vue1.0版本)或axios(vue2.0版本)第三方插件的支持才行
    2018-10-10
  • Vue 組件注冊(cè)全解析

    Vue 組件注冊(cè)全解析

    這篇文章主要介紹了Vue 組件注冊(cè)全解析的相關(guān)資料,幫助大家更好的理解和使用vue,感興趣的朋友可以了解下
    2020-12-12
  • Vue自定義指令實(shí)現(xiàn)卡片翻轉(zhuǎn)功能

    Vue自定義指令實(shí)現(xiàn)卡片翻轉(zhuǎn)功能

    這篇文章主要給大家介紹了Vue自定義指令實(shí)現(xiàn)卡片翻轉(zhuǎn)功能的代碼示例,文章通過(guò)代碼示例講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的參幫助,需要的朋友可以參考下
    2023-11-11
  • 詳解vue3的沙箱機(jī)制

    詳解vue3的沙箱機(jī)制

    這篇文章主要介紹了vue3的沙箱機(jī)制的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用vue框架,感興趣的朋友可以了解下
    2021-04-04
  • vue使用原生js創(chuàng)建元素樣式不生效問(wèn)題及解決

    vue使用原生js創(chuàng)建元素樣式不生效問(wèn)題及解決

    這篇文章主要介紹了vue使用原生js創(chuàng)建元素樣式不生效問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • vue+springboot用戶(hù)注銷(xiāo)功能實(shí)現(xiàn)代碼

    vue+springboot用戶(hù)注銷(xiāo)功能實(shí)現(xiàn)代碼

    這篇文章主要介紹了vue+springboot用戶(hù)注銷(xiāo)功能,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2024-05-05
  • Vue3非遞歸渲染Tree組件的初步實(shí)現(xiàn)代碼

    Vue3非遞歸渲染Tree組件的初步實(shí)現(xiàn)代碼

    這篇文章主要介紹了Vue3非遞歸渲染Tree組件的初步實(shí)現(xiàn),文中通過(guò)代碼示例講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定幫助,需要的朋友可以參考下
    2024-05-05
  • vue/cli3版本打包如何去掉soucemap文件

    vue/cli3版本打包如何去掉soucemap文件

    這篇文章主要介紹了vue/cli3版本打包如何去掉soucemap文件問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • Vue.js實(shí)現(xiàn)分頁(yè)查詢(xún)功能

    Vue.js實(shí)現(xiàn)分頁(yè)查詢(xún)功能

    這篇文章主要為大家詳細(xì)介紹了Vue.js實(shí)現(xiàn)分頁(yè)查詢(xún)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • vue 動(dòng)態(tài)生成拓?fù)鋱D的示例

    vue 動(dòng)態(tài)生成拓?fù)鋱D的示例

    這篇文章主要介紹了vue 動(dòng)態(tài)生成拓?fù)鋱D的示例,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下
    2021-01-01

最新評(píng)論

句容市| 巴马| 应城市| 屯昌县| 秦皇岛市| 松原市| 称多县| 丹阳市| 曲阜市| 肃宁县| 新密市| 武宣县| 辰溪县| 通山县| 项城市| 晴隆县| 九寨沟县| 绥德县| 江津市| 阜阳市| 时尚| 扶风县| 白河县| 黄梅县| 曲阳县| 西畴县| 南平市| 长阳| 普安县| 塔河县| 霍城县| 苗栗县| 信丰县| 乌兰浩特市| 溧阳市| 洪雅县| 皋兰县| 通州区| 枞阳县| 大竹县| 正宁县|