VUE3使用JSON編輯器方式
1、先看看效果圖
可以自行選擇展示效果


2、這是我在vue3項(xiàng)目中使用的JSON編輯器
首先引入第三方插件
npm install json-editor-vue3 yarn add json-editor-vue3
3、引入到項(xiàng)目中
// 導(dǎo)入模塊
import JsonEditorVue from 'json-editor-vue3'
// 注冊(cè)組件
components: { JsonEditorVue },
4、一般后端返回的是會(huì)將JSON轉(zhuǎn)為String形式
我們傳給后端也是通過(guò)這種形式
就可以通過(guò)后端拿到的數(shù)據(jù)進(jìn)行JSON與String之間轉(zhuǎn)換
// 后端拿到的數(shù)據(jù)
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\"}}"
// 我們通過(guò)JSON.parse()進(jìn)行轉(zhuǎn)換
let isJson = JSON.parse(configValue) // 這樣我們拿到的就是JSON格式的了,可以渲染出來(lái)的
// 我們傳給后端的數(shù)據(jù)也要將JSON轉(zhuǎn)成字符串,通過(guò)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>
// 導(dǎo)入模塊
import JsonEditorVue from 'json-editor-vue3'
export default defineComponent({
name: 'EnterpriseList',
// 注冊(cè)組件
components: {
JsonEditorVue,
},
setup() {
const state = reactive({
currentMode: 'tree'
})
return {
...toRefs(state),
}
},
})
}
</script>
6、參數(shù)
| 參數(shù) | 類型 | 描述 | 默認(rèn) |
|---|---|---|---|
| modelValue | Object | 要編輯的json值 | |
| options | Object | jsoneditor的options,參考configuration-options | |
| currentMode | String | 當(dāng)前編輯模式 | code |
| modeList | Array | 可選的編輯模式列表 | [“tree”, “code”, “form”, “text”, “view”] |
| language | Array | 語(yǔ)言 | en |
7、事件
| Name | Description |
|---|---|
| update:modelValue | json 更新 |
| change | json 更新 |
| textSelectionChange | 參考configuration-options對(duì)應(yīng)參數(shù),參數(shù)有重寫,第一個(gè)參數(shù)為編輯器的實(shí)例,后續(xù)參數(shù)與官方參數(shù)相同 |
| selectionChange | 參考configuration-options對(duì)應(yīng)參數(shù),參數(shù)有重寫,第一個(gè)參數(shù)為編輯器的實(shí)例,后續(xù)參數(shù)與官方參數(shù)相同 |
| focus | 參考configuration-options對(duì)應(yīng)參數(shù),參數(shù)有重寫,第一個(gè)參數(shù)為編輯器的實(shí)例,后續(xù)參數(shù)與官方參數(shù)相同 |
| blur | 參考configuration-options對(duì)應(yīng)參數(shù),參數(shù)有重寫,第一個(gè)參數(shù)為編輯器的實(shí)例,后續(xù)參數(shù)與官方參數(shù)相同 |
| colorPicker | 參考configuration-options對(duì)應(yīng)參數(shù),參數(shù)有重寫,第一個(gè)參數(shù)為編輯器的實(shí)例,后續(xù)參數(shù)與官方參數(shù)相同 |
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue3安裝dataV報(bào)錯(cuò)問(wèn)題解決方案
這篇文章主要給大家介紹了關(guān)于Vue3安裝dataV報(bào)錯(cuò)問(wèn)題解決方案的相關(guān)資料,dataV用于大屏展示,個(gè)人覺(jué)得比echarts簡(jiǎn)單很多,需要的朋友可以參考下2023-06-06
webpack+vuex+axios 跨域請(qǐng)求數(shù)據(jù)的示例代碼
詳解Vue的watch中的immediate與watch是什么意思
Vue動(dòng)態(tài)類的幾種使用方法總結(jié)
解決vue.js出現(xiàn)Vue.js not detected錯(cuò)誤的問(wèn)題
vue導(dǎo)出少量pdf文件實(shí)現(xiàn)示例詳解

