Vue3 使用json编辑器

安装

npm install json-editor-vue3

main中引入

main.js 中加入下面代码

javascript 复制代码
import "jsoneditor";

不然会有报错,如jsoneditor does not provide an export named 'default'。 图片信息来源-github

代码示例

javascript 复制代码
<template>
    <json-editor-vue class="editor" v-model="jsonobj" @blur="remarkValidate" currentMode="text" :modeList="modeList"
        :options="options" />
</template>

<script setup>
import JsonEditorVue from 'json-editor-vue3';
import { ref } from 'vue'

const jsonstr = ref("{ \"a\": 8, \"b\": \"2\", \"c\":8, \"d\":9, \"f\":99 }");
const jsonobj = ref(JSON.parse(jsonstr.value));

const options = ref({
    search: false,
    history: false,
})
const modeList= ref(["text", "view", "tree", "code", "form"]) // 可选模式

const remarkValidate = () => {
    let newjsonstr = JSON.stringify(jsonobj.value);
    console.log("remarkValidate", jsonobj.value, newjsonstr, jsonstr.value);
    if (jsonstr.value == newjsonstr) {
        console.log("no change")
    } else {
        jsonstr.value = newjsonstr
    }
}
</script>

补充说明

json-editor-vue3支持多种配置,如可选模式(多选)modeList、初始模式currentMode,历史记录开关history,搜索功能开关search等, 上面示例代码已做部分配置实验,具体可以参考github的配置介绍。

运行结果

相关推荐
salestina4 分钟前
vscode迁移扩展目录
ide·vscode·编辑器
豆沙沙包?5 分钟前
函数重载/类和对象(P14-P60)
前端·算法
丁婷婷16 分钟前
Reflect 在 Vue3 响应式中的作用是什么?
vue.js
三十而立洋21 分钟前
彻底搞懂跨域:原理、CORS、解决方案与实战避坑
前端
xiaominlaopodaren21 分钟前
three.js最小地图运行时(十):接入离线逻辑瓦片剖面
javascript·gis·three.js
光泽雨23 分钟前
JSON /XML转换
xml·json
计算机魔术师23 分钟前
ASR转录总出错?Google新模型WER降到2.6%,还能自动帮你改口误
前端
吃西瓜的年年24 分钟前
vue3原理思维导图
vue.js·笔记·flutter
xcs1940525 分钟前
新版 IDEA(尤其是 2024/2025/2026)越来越臃肿
前端·人工智能·intellij-idea
Patrick_Wilson25 分钟前
iOS 第三方浏览器图片下载失败问题
前端·ios·浏览器