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>
相关推荐
fruge1 小时前
2025前端工程化与性能优化实战指南:从构建到监控的全链路方案
前端·性能优化
aesthetician2 小时前
Node.js v25 重磅发布!革新与飞跃:深入探索 JavaScript 运行时的未来
javascript·node.js·vim
Han.miracle3 小时前
数据结构——二叉树的从前序与中序遍历序列构造二叉树
java·数据结构·学习·算法·leetcode
Le1Yu4 小时前
分布式事务以及Seata(XA、AT模式)
java
寒山李白5 小时前
关于Java项目构建/配置工具方式(Gradle-Groovy、Gradle-Kotlin、Maven)的区别于选择
java·kotlin·gradle·maven
demi_meng5 小时前
reactNative 遇到的问题记录
javascript·react native·react.js
无妄无望5 小时前
docker学习(4)容器的生命周期与资源控制
java·学习·docker
MC丶科6 小时前
【SpringBoot 快速上手实战系列】5 分钟用 Spring Boot 搭建一个用户管理系统(含前后端分离)!新手也能一次跑通!
java·vue.js·spring boot·后端
千码君20166 小时前
React Native:从react的解构看编程众多语言中的解构
java·javascript·python·react native·react.js·解包·解构
夜白宋7 小时前
【word多文档docx合并】
java·word