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>
相关推荐
!win !2 小时前
前端跨标签页通信方案(下)
前端·javascript
f***45322 小时前
基于SpringBoot和PostGIS的各省与地级市空间距离分析
android·前端·后端
编码追梦人3 小时前
从 “手忙脚乱“ 到 “行云流水“:华为云 DevUI 与 MateChat 如何让前端开发飞起来
前端·华为云
用户47949283569153 小时前
TypeScript 简史:它是怎么拯救我的烂代码的
javascript·typescript
S***H2833 小时前
前端动画实现经验,性能优化与兼容性
前端
用户47949283569154 小时前
只有前端 Leader 才会告诉你:那些年踩过的模块加载失败的坑(二)
javascript
xw54 小时前
前端跨标签页通信方案(下)
前端·javascript
zzlyx994 小时前
IoTSharp前端VUE采用npm run build编译提示require() of ES Module 出错
前端·vue.js·npm
全栈技术负责人4 小时前
拒绝“无法复现”:前端全链路日志排查实战手册
前端·全链路·问题排查思路
加洛斯4 小时前
前端小知识003:JS中 == 与 === 的区别
开发语言·前端·javascript