首頁  >  文章  >  web前端  >  VUE3怎麼使用JSON編輯器

VUE3怎麼使用JSON編輯器

PHPz
PHPz轉載
2023-05-12 17:34:062566瀏覽

1、先看看效果圖,可以自行選擇展示效果

VUE3怎麼使用JSON編輯器

2、這是我在vue3專案中使用的JSON編輯器,首先引入第三方外掛

npm install json-editor-vue3

yarn add json-editor-vue3

3、引入到專案中

// 导入模块
import JsonEditorVue from 'json-editor-vue3'

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

4、一般後端回傳的是會將JSON轉為String形式

我們傳給後端也是透過這種形式,就可以透過後端拿到的資料進行JSON與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、範例:

<template>
  <div>
    <json-editor-vue
        v-model="jsonData"
        class="editor"
        :current-mode="currentMode"
    />
  </div>
</template>
 
<script>
  // 导入模块
  import JsonEditorVue from &#39;json-editor-vue3&#39;
 
  export default defineComponent({
    name: &#39;EnterpriseList&#39;,
    //  注册组件
    components: {
      JsonEditorVue,
    },
    setup() {
      const state = reactive({
        currentMode: &#39;tree&#39;
      })
      return {
        ...toRefs(state),
      }
    },
  })
  }
</script>

6、參數

##String目前編輯模式 #code[“tree”, “code”, “ form”, “text”, “view”]
參數 類型 描述 預設
modelValue Object 要編輯的json值
#options Object jsoneditor的options,參考configuration-options
#currentMode
modeList Array 可選的編輯模式清單

language#Array7、事件Namejson 更新json 更新參考configuration-options對應參數,參數有重寫,第一個參數為編輯器的實例,後續參數與官方參數相同#參考configuration-options對應參數,參數有重寫,第一個參數為編輯器的實例,後續參數與官方參數相同##參考configuration-options對應參數,參數有重寫,第一個參數為編輯器的實例,後續參數與官方參數相同
語言 en
#Description
#update:modelValue
change
textSelectionChange
selectionChange
focus
###blur######參考configuration-options對應參數,參數有重寫,第一個參數為編輯器的實例,後續參數與官方參數相同#############colorPicker######參考configuration-options對應參數,參數有重寫,第一個參數為編輯器的實例,後續參數與官方參數相同#############

以上是VUE3怎麼使用JSON編輯器的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除