Vue3 使用json编辑器

安装

npm install json-editor-vue3

main中引入

main.js 中加入下面代码

import "jsoneditor";

不然会有报错,如jsoneditor does not provide an export named ‘default’。 图片信息来源-github
在这里插入图片描述

代码示例


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

运行结果

![在这里插入图片描述](https://img-blog.csdnimg.cn/3fa27463c42740bc9109d6c6cfb184c1.pn

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值