vue3 使用 jsoneditor

vue3 使用 jsoneditor

在main.js中引入 样式文件

javascript 复制代码
import 'jsoneditor/dist/jsoneditor.css'

复制代码放到文件中就能用了

jsoneditor.vue

javascript 复制代码
<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>
相关推荐
阿正的梦工坊1 小时前
JavaScript 微任务与宏任务完全指南
开发语言·javascript·ecmascript
懂懂tty1 小时前
CRA 迁移 Rspack(实战)
前端·架构
小码哥_常2 小时前
Kotlin 助力 Android 启动“大提速”
前端
GreenTea3 小时前
AI 时代,工程师的不可替代性在哪里
前端·人工智能·后端
Jagger_3 小时前
能不能别再弄低代码害人了
前端
朦胧之3 小时前
AI 编程开发思维
前端·后端·ai编程
踩着两条虫4 小时前
VTJ:快速开始
前端·低代码·架构
木斯佳5 小时前
前端八股文面经大全:携程前端一面(2026-04-17)·面经深度解析
前端·状态模式
2301_799073025 小时前
基于 Next.js + 火山引擎 AI 的电商素材智能生成工具实战——字节跳动前端训练营成果
javascript·人工智能·火山引擎
Java后端的Ai之路5 小时前
LangChain ReAct Agent 核心技术问答
前端·react.js·langchain