get与post如何拼接url与数据的灵活处理,循环的重要性。

get与post拼接url地址不同:

javascript 复制代码
 let postData = {
        method: "post",
        data: {
          op: "/api/setting/maintenanceperiod?period="+this.authorizationCode,
          loadingConfig: {},
          data: {
            period:this.authorizationCode
          }
        }
      };
javascript 复制代码
if(this.editData.id){
                  let postData = {
                    method: "get",
                    data: {
                      op: "/api/" + this.editData.id + "/rmmc",
                      loadingConfig: {},
                      data: {
                        id: this.editData.id,
                        pwd: this.editData.password,
                      },
                    },
                  };

后端返回一个:

某某市领导职数共20个,实配2,缺编<span style='color:#D1881B'>18</span>个,

进行页面渲染,有3种方式,第一种最笨的方式,自己手动敲出来,但是不够灵活,一旦后端的数据要变,可采用第二种,分割开来赋值。第三种相对灵活,直接循环分割的字符串,进行v-html赋值,并绑定样式。

javascript 复制代码
 <div class="actual" v-if="returnZsbzText">
                      <img src="../../../../static/images/head-renyuan.png" alt="">
                      <!-- <span>一二级主任科员职数999个,</span>
                      <span>实有999个,</span>
                      <span>空缺999个</span> -->

                      <!-- <span v-html="returnZsbzText.split(',')[0]"></span>
                      <span v-html="returnZsbzText.split(',')[1]"></span>
                      <span v-html="returnZsbzText.split(',')[2]" :style="{ color: '#D1881B' }"></span>
                      <span v-html="returnZsbzText.split(',')[3]" :style="{ color: '#4177c7' }"></span> -->

                      <template v-for="sentence in returnZsbzText.split(',')">
                        <span v-html="sentence" :style="getStyle(sentence)" :key=""></span>
                      </template>
                    </div>
javascript 复制代码
 getStyle(sentence) {
      if (sentence.includes("style")) {
        // 提取style样式
        const styleStartIndex = sentence.indexOf('style="') + 7;
        const styleEndIndex = sentence.indexOf('"', styleStartIndex);
        const style = sentence.slice(styleStartIndex, styleEndIndex);

        // 解析样式字符串,提取color属性的值
        const colorStartIndex = style.indexOf("color:") + 6;
        const colorEndIndex = style.indexOf(";", colorStartIndex);
        const color = style.slice(colorStartIndex, colorEndIndex).trim();

        return { color: color };
      } else {
        return {}; // 默认样式
      }
    },

此时,若后端返回的是一个数组,数组里面是两条类似于以上的数据

"一二级主任科员职数0个,实有0个,;", "三四级主任科员职数0个,实有0个,;"

此时采用相同的方式,再加一个for循环:

javascript 复制代码
     <div class="actualVacancy" v-if="returnRybzText">
         
                      <div class="actual" v-for="(item,index) in returnRybzText" :key="index">
                        <img src="../../../../static/images/head-renyuan.png" alt="">
                        <template v-for="sentence in item.split(',')">
                          <span v-html="sentence" :style="getStyle(sentence)" :key=""></span>
                        </template>
                      </div>
                      
                  </div>
相关推荐
pixle07 分钟前
Vue3 Echarts 3D饼图(3D环形图)实现讲解附带源码
前端·3d·echarts
麻芝汤圆41 分钟前
MapReduce 入门实战:WordCount 程序
大数据·前端·javascript·ajax·spark·mapreduce
juruiyuan1112 小时前
FFmpeg3.4 libavcodec协议框架增加新的decode协议
前端
熊大如如3 小时前
Java 反射
java·开发语言
Peter 谭3 小时前
React Hooks 实现原理深度解析:从基础到源码级理解
前端·javascript·react.js·前端框架·ecmascript
猿来入此小猿3 小时前
基于SSM实现的健身房系统功能实现十六
java·毕业设计·ssm·毕业源码·免费学习·猿来入此·健身平台
周胡杰3 小时前
鸿蒙接入flutter环境变量配置windows-命令行或者手动配置-到项目的创建-运行demo项目
javascript·windows·flutter·华为·harmonyos·鸿蒙·鸿蒙系统
goTsHgo3 小时前
Spring Boot 自动装配原理详解
java·spring boot
卑微的Coder4 小时前
JMeter同步定时器 模拟多用户并发访问场景
java·jmeter·压力测试
pjx9874 小时前
微服务的“导航系统”:使用Spring Cloud Eureka实现服务注册与发现
java·spring cloud·微服务·eureka