Vue3中jsoneditor的使用示例詳解
更新時間:2024年01月11日 16:42:24 作者:Mrceel
這篇文章主要為大家詳細介紹了Vue3中jsoneditor的使用相關(guān)知識,文中的示例代碼講解詳細,具有一定的學習價值,感興趣的小伙伴可以跟隨小編一起了解下
vue3 使用 jsoneditor
復制代碼放到文件中就能用了
jsoneditor.vue
<template>
<div ref="jsonDom" style="width: 100%; height: 460px"></div>
</template>
<script setup lang="ts">
import { ref, onMounted, watchEffect } from 'vue'
import JsonEditor from 'jsoneditor'
interface validateResult {
path: Array<string | number>
message: string
}
const props = defineProps<{
option: any
validate?: (val: any) => validateResult
}>()
const emit = defineEmits(['update:modelValue', 'change', 'customValidation'])
const jsonDom = ref(null)
const validate = (res: any, editor: any) => {
try {
emit('change', {
success: res.length === 0 && typeof editor.get() !== 'number',
json: editor.getText()
})
} catch (error) {
console.log(error)
}
}
onMounted(() => {
const options = {
history: false,
sortObjectKeys: false,
mode: 'code',
modes: ['code', 'text'],
onChange() {
editor.validate().then((res: any) => validate(res, editor))
},
onBlur() {
try {
editor.set(eval(`(${editor.getText()})`))
editor.validate().then((res: any) => validate(res, editor))
} catch (error) {
console.log(error)
}
},
onValidate: props.validate,
onValidationError: function (errors: any) {
errors.forEach((error: any) => {
switch (error.type) {
case 'validation': // schema validation error
break
case 'customValidation': // custom validation error
emit('customValidation')
break
case 'error': // json parse error
emit('change', {
success: false,
json: editor.getText()
})
break
}
})
}
}
const editor = new JsonEditor(jsonDom.value, options)
watchEffect(() => {
editor.set(props.option)
editor.validate().then((res: any) => validate(res, editor))
})
})
</script>
到此這篇關(guān)于Vue3中jsoneditor的使用示例詳解的文章就介紹到這了,更多相關(guān)Vue3 jsoneditor內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
el-select如何獲取下拉框選中l(wèi)abel和value的值
在開發(fā)業(yè)務場景中我們通常遇到一些奇怪的需求,例如el-select業(yè)務場景需要同時獲取我們選中的label跟 value,下面這篇文章主要給大家介紹了關(guān)于el-select如何獲取下拉框選中l(wèi)abel和value的值,需要的朋友可以參考下2022-10-10
在Vue3中創(chuàng)建和使用自定義指令的實現(xiàn)方式
在現(xiàn)代前端開發(fā)中,Vue.js 是一個非常流行的框架,它提供了許多強大的功能來幫助開發(fā)者構(gòu)建高效的用戶界面,自定義指令是 Vue.js 的一個重要特性,它允許開發(fā)者擴展 HTML 元素的功能,本文將詳細介紹如何在 Vue3 中創(chuàng)建和使用自定義指令,并提供示例代碼來幫助理解2024-12-12

