SoFunction
Updated on 2025-04-03

Detailed explanation of the use example of jsoneditor in Vue3

vue3 uses jsoneditor

Copy the code and put it in the file to use it

<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:  === 0 && typeof () !== 'number',
            json: ()
        })
    } catch (error) {
        (error)
    }
}
onMounted(() => {
    const options = {
        history: false,
        sortObjectKeys: false,
        mode: 'code',
        modes: ['code', 'text'],
        onChange() {
            ().then((res: any) => validate(res, editor))
        },
        onBlur() {
            try {
                (eval(`(${()})`))
                ().then((res: any) => validate(res, editor))
            } catch (error) {
                (error)
            }
        },
        onValidate: ,
        onValidationError: function (errors: any) {
            ((error: any) => {
                switch () {
                    case 'validation': // schema validation error
                        break
                    case 'customValidation': // custom validation error
                        emit('customValidation')
                        break
                    case 'error': // json parse error
                        emit('change', {
                            success: false,
                            json: ()
                        })
                        break
                }
            })
        }
    }
    const editor = new JsonEditor(, options)
    watchEffect(() => {
        ()
        ().then((res: any) => validate(res, editor))
    })
})
</script>

This is the end of this article about the detailed explanation of the use examples of jsoneditor in Vue3. For more related content of Vue3 jsoneditor, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!