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

运行结果

相关推荐
陈随易20 小时前
在 VSCode 里,把项目一键部署到服务器
前端·后端·程序员
Highcharts.js20 小时前
教程:基于 React + Highcharts 构建一个单页应用程序、按需数据拉取与图表渲染
开发语言·前端·数据结构·react.js·前端框架·highcharts·页面应用
To_OC21 小时前
我被 useState 坑了两次之后,终于把它的脾气摸透了
前端·javascript·react.js
鱼樱前端21 小时前
我用 Claude Code 后,编码效率翻 3 倍。但更值钱的是别的。
前端·后端·ai编程
_lucas21 小时前
给知识库网站接入AI问答
前端·ai编程·全栈
前端糕手1 天前
前端面试题大全:JavaScript + Vue3 + React + TypeScript + 工程化 + 性能优化
前端
我不叫武1 天前
一个用 Rust 写的离线编码查询桌面工具
前端
Web4Browser1 天前
指纹浏览器 API 自动化怎么接:启动 Profile、获取 CDP 端点并连接自动化框架
前端·网络·typescript·自动化
Patrick_Wilson1 天前
从 React 到 Flutter:写给前端的一张跨端知识地图
前端·flutter·react.js
颜酱1 天前
06 | 把 meta_config 同步进 MySQL(生成阶段)
前端·人工智能·后端