JSON editor used by VUE2

1. Take a look at the effect picture first, and you can choose to display the effect by yourself
insert image description here
insert image description here

2. This is the JSON editor I use in the vue2 project, first introduce the third-party plug-in

npm install vue-json-editor --save

3. Introduce it into the project (the complete code will be placed at the bottom, and it can be used directly after installation)

// 导入模块
import vueJsonEditor from 'vue-json-editor'

// 注册组件
components: {
    
     vueJsonEditor },

4. Generally, what the backend returns is to convert JSON into a String form. We pass it to the backend through this form, and the data obtained by the backend can be converted between JSON and String.

// 后端拿到的数据
configValue:"{\"isBigTree\":true,\"needContact\":true,\"needProvinceCity\":true,\"needDetailAddress\":true,\"needReservationCheckSms\":false,\"BigTreeReservationConfig\":{\"orderApiUrl\":\"https://api.bigtreedev.com/openplatform/openApi/api/order/create/notification/v001?sign=\",\"reservationApiUrl\":\"https://api.bigtreedev.com/openplatform/openApi/api/service/appointment/create/service/appointment/v001?sign=\",\"cancelApiUrl\":\"https://api.bigtreedev.com/openplatform/openApi/api/order/unsubscribe/notification/v001?sign=\",\"companyNo\":\"C400020\",\"verNo\":\"v001\",\"secretKey\":\"72CDFFD7F63D8662B6E1873FEA14EB24\",\"signSecretId\":\"0BBF774D11C0A053A6C2A2E36E6C6C2E2C55D483\"}}"
// 我们通过JSON.parse()进行转换
let isJson = JSON.parse(configValue) // 这样我们拿到的就是JSON格式的了,可以渲染出来的
// 我们传给后端的数据也要将JSON转成字符串,通过JSON.stringify()
let isString = JSON.stringify(configValue)  // 这样我们拿到的就是String格式的了,直接传给后端

5. The following is the complete code (with comments written)

<template>
  <div>
    <vue-json-editor
      v-model="resultInfo"
      :showBtns="false"
      :mode="'code'"
      @json-change="onJsonChange"
      @json-save="onJsonSave"
      @has-error="onError"
    />
    <br>
    <el-button type="primary" @click="checkJson">确定</el-button>
  </div>
</template>
 
<script>
  // 导入模块
  import vueJsonEditor from 'vue-json-editor'
 
  export default {
    
    
    // 注册组件
    components: {
    
     vueJsonEditor },
    data() {
    
    
      return {
    
    
        hasJsonFlag:true,  // json是否验证通过
        // json数据
        resultInfo: {
    
    "isBigTree":true,"needContact":true,"needProvinceCity":true,"needDetailAddress":true,"BigTreeReservationConfig":{
    
    "orderApiUrl":"https://test.api.bigtreedev.com/openplatform/openApi/api/order/create/notification/v001?sign=","reservationApiUrl":"https://test.api.bigtreedev.com/openplatform/openApi/api/service/appointment/create/service/appointment/v001?sign=","cancelApiUrl":"https://test.api.bigtreedev.com/openplatform/openApi/api/order/unsubscribe/notification/v001?sign=","ServiceProcessQueryApiUrl":"https://test.api.bigtreedev.com/openplatform/openApi/service/appointment/getServiceProcessInfo/v001?sign=","companyNo":"C400020","verNo":"v001","secretKey":"B2DE13A4D7D8600C1332D1A7D6C948F3","signSecretId":"EB7A2223A096E4DCE14C50D924582BEBABE653AF","whiteListSynchroUrl":"https://test.api.bigtreedev.com/openplatform/openApi/api/whiteList/create/notification/v001?sign=","whiteListCorrectingUrl":"https://test.api.bigtreedev.com/openplatform/openApi/api/whiteList/update/notification/v001?sign=","whiteListOptOutUrl":"https://test.api.bigtreedev.com/openplatform/openApi/api/whiteList/unsubscribe/notification/v001?sign=","whiteListRenewalUrl":"https://test.api.bigtreedev.com/openplatform/openApi/api/whiteList/renewal/notification/v001?sign="}}
      }
    },
    mounted: function() {
    
    
    },
    methods: {
    
    
      onJsonChange (value) {
    
    
        // console.log('更改value:', value);
        // 实时保存
        this.onJsonSave(value)
      },
      onJsonSave (value) {
    
    
        // console.log('保存value:', value);
        this.resultInfo = value
        this.hasJsonFlag = true
      },
      onError(value) {
    
    
        // console.log("json错误了value:", value);
        this.hasJsonFlag = false
      },
      // 检查json
      checkJson(){
    
    
        if (this.hasJsonFlag == false){
    
    
          // console.log("json验证失败")
          // this.$message.error("json验证失败")
          alert("json验证失败")
          return false
        } else {
    
    
          // console.log("json验证成功")
          // this.$message.success("json验证成功")
          alert("json验证成功")
          return true
        }
      },
    }
  }
</script>
 
<style>
</style>

Guess you like

Origin blog.csdn.net/weixin_44949068/article/details/129358878