promise.all使用

处理一组promise的异步请求,通过每张图片的前缀url,依次异步请求获取图片自己的base64码然后渲染图片

//index.vue
//template
  <li class="policy-img" v-for="(it,index) in imageInfos">
            <img :src="it.imgUrl" alt/>
            <i @click="imageInfos.splice(index,1)">×</i>
          </li>
//js methods
  getImgB64Str(imageInfos) {
        let param = {
          accessToken: sessionStorage.getItem("accessToken"),
          accountId: sessionStorage.getItem("accountId")
        };
        let promiseArr = [];
        imageInfos.forEach(it => {
          // param.imgUrl = it.imgUrl;
          // param.policyId = this.policyId;
          let _param = {...param, imgUrl: it.imgUrl, policyId: this.policyId}
          promiseArr.push(policyManageApi.getImgB64Str(baseUrl, _param));
        });
        Promise.all(promiseArr).then(
          res => {
            this.otherInfo.imageInfos = res.map(resRut => {
              let temObj = {};
              if (resRut.data.code == "0200") {
                temObj.imgUrl = `data:image/png;base64,${resRut.data.data.imgBase64String}`;
              } else {
                this.showPosition("middle", resRut.data.message);
              }
              return temObj;
            });
          },
          err => {
          }
        );
      },

//调用方法
this.getImgB64Str(this.imageInfos);

 

上一篇:使用pdf.js 预览pdf文件(BASE64格式)前端


下一篇:路径资源统一处理、加工、拼接方法