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的配置介绍。

运行结果

相关推荐
卓怡学长几秒前
w266基于spring boot + vue 圣地延安美食乐享系统
java·数据库·vue.js·spring boot·spring·intellij-idea
minglie121 分钟前
npm ali-oss库依赖冲突
前端·npm
CryptoPP37 分钟前
BSE股票K线数据接入实战:从接口调用到前端图表展示
大数据·前端·网络·人工智能·websocket·网络协议
没落英雄2 小时前
5. 从零开始搭建一个 AI Agent —— 人机协作与中断恢复
前端·人工智能·架构
品尚公益团队3 小时前
C#在asp.net网页开发中的带cookie登录实现
前端·c#·asp.net
多加点辣也没关系4 小时前
JavaScript|第9章:对象 — 基础
开发语言·javascript·ecmascript
没有了遇见4 小时前
AI Agent 是什么?—— 一文理解 LLM、Memory、Skills、Tools、MCP、Workflow,Context
android·前端·程序员
用户83134859306984 小时前
Cesium自定义可调节星空夜景+星星闪烁
vue.js·cesium
SelectDB技术团队4 小时前
Agent 场景动态 JSON 性能拆解:Apache Doris 比 ClickHouse 快 7 倍、比 Elasticsearch 快 2 倍
数据库·clickhouse·elasticsearch·json·apache·日志分析·apache doris
后台开发者Ethan4 小时前
setState组件更新
前端·javascript·react