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

运行结果

相关推荐
风语者日志5 分钟前
[LitCTF 2023]作业管理系统
前端·网络·安全·web安全·ctf
咖啡の猫11 分钟前
vue 项目中常用的 2 个 Ajax 库
vue.js·ajax·okhttp
excel15 分钟前
深入解析:Vue 编译器核心工具函数源码(compiler-core/utils.ts)
前端
excel16 分钟前
第五章:辅助函数与全流程整合
前端
excel17 分钟前
🔍 深度解析:Vue 编译器中的 validateBrowserExpression 表达式校验机制
前端
excel17 分钟前
深度解析:Vue 模板编译器中的 Tokenizer 实现原理
前端
excel20 分钟前
🧩 Vue 编译核心:transform.ts 源码深度剖析
前端
excel20 分钟前
Vue Runtime Helper 常量与注册机制源码解析
前端
excel23 分钟前
Vue 模板编译器核心选项解析:从 Parser 到 Codegen 的全链路设计
前端
excel23 分钟前
第四章:表达式与循环解析函数详解
前端