vue 格式化显示对比json串

1.使用插件 npm install vue-code-diff
2.引入插件

import CodeDiff from "vue-code-diff";
export default {
  components: { CodeDiff },
  data() {
    return {
      oldStr: "",
      newStr: ""
    };
  },

3.template使用

<div>
      <code-diff
        :old-string="oldStr"
        :new-string="newStr"
        outputFormat="side-by-side"
        :context="10"
      >
      </code-diff>
    </div>

4.重点来了,格式化

this.oldStr = JSON.stringify(response.data,null,4);
this.newStr = JSON.stringify(response.data.data,null,4);

5.效果显示
在这里插入图片描述
6.整体代码

<!--
 * @Description: 
 * @Autor: wolfslians
 * @Date: 2020-04-13 17:30:00
 * @LastEditors: wolfslians
 * @LastEditTime: 2020-04-13 23:46:18
 -->
<template>
  <section>
    <div>
      <code-diff
        :old-string="oldStr"
        :new-string="newStr"
        outputFormat="side-by-side"
        :context="10"
      >
      </code-diff>
    </div>
  </section>
</template>
<script>
import CodeDiff from "vue-code-diff";
import { scenariotest_api } from "@/api";
export default {
  components: { CodeDiff },
  data() {
    return {
      oldStr: "",
      newStr: ""
    };
  },
  mounted() {
    this.getgrouplist();
  },
  methods: {
    getgrouplist() {
      scenariotest_api.grouplist().then(response => {
        var data = response.data;
        if (data.code == 200) {
          this.oldStr = JSON.stringify(response.data,null,4);
          this.newStr = JSON.stringify(response.data.data,null,4);
        } else if (data.code === 401) {
          this.$router.push({
            path: "/Permission/NoPermission"
          });
        } else {
          this.$Message.error({
            content: response.data.msg,
            background: true,
            duration: 3
          });
        }
      });
    },
  }
};
</script>
<style scoped>
</style>
Logo

前往低代码交流专区

更多推荐