vue中使用json-editor个人总结

jdorn/json-edior在线体验

json编辑器有很多种,这边为了呈现给用户看,所以无法使用原始的json编辑器,于是使用的是jdorn/json-editor
在这里插入图片描述
相比较而言,这款编辑器更适合用户操作,之前考虑过 jsoneditorvue-json-viewer,发现这两款更适合有代码基础的人来使用,于是没有使用这两款json编辑器.

使用json-editor时参考了以下两篇博客

JSON Editor 中文文档
Vue 项目使用 json-editor (一)

在这里插入图片描述
不过按照上叙博客中的搭建方法会发现this上没有挂载到JSONEdior方法,于是根据import '@json-editor/json-editor'找到了这个js文件,发现这个日期有点感人,比我还先出生.
在这里插入图片描述
然后拉了官方实例,将官方实例中的js文件拷贝到assets或者plugins文件夹下,在需要使用的地方import "@/plugins/jsoneditor.js";引入即可,然后打印this或者window,就会发现挂载到了JSONEditor方法.就可以正常调用了,最后把这个整理成了一个组件,按需调用

此处采用的foundation5主题,如果要使用,需要下载相关css文件,不然可能显示有问题
组件

<template>
    <div class="json_editor">
        <div id="editor_holder"></div>
        <div class="editor-button">
            <el-button type="success">提交</el-button>
            <el-button type="danger">取消</el-button>
        </div>
    </div>
</template>
<script>
//根据自己的相关文件位置找到对应路径
import "@/plugins/jsoneditor.js";  
import "@/assets/css/foundation.min.css";
import "@/assets/css/font-awesome.css";

export default {
    props: {
        jsonData: {
            type: Object,
            default: () => {},
        },
    },
    data() {
        return {};
    },
    mounted() {
        let schema = {
            title: "Person",
            type: "object",
            properties: {},
        };
        let element = window.document.getElementById("editor_holder");
        let config = {
            schema: {},
            theme: "foundation5",       //设置主题,可以是bootstrap或者jqueryUI等
            iconlib: "fontawesome4",    //设置字体
            disable_properties: true,   //如果设置为 true, 将隐藏编辑属性按钮.
            // required_by_default: true,
            // disable_edit_json: true, //如果设置为 true, 将隐藏 Edit JSON 按钮.
            object_layout: "grid",      //属性为object时,属性默认normal,设置grid可以一排多个
            disable_collapse: true,     //如果设置为 true, 对象和数组不再显示“折叠”按钮.
        };
        config["schema"] = schema;
        this.editor = new window.JSONEditor(element, config);
        let data = this.jsonData
        this.editor.setValue({ data });
    },
    methods: {},
};
</script>
<style lang="scss" scoped>
.json_editor {
    .editor-button {
        width: 1170px;
        margin: auto;
    }
}
</style>

调用处

<template>
    <div class="pratice">
        <JsonEditor :jsonData="jsonData"/>
    </div>
</template>
<script>
import JsonEditor from "@/components/JsonEditor";
export default {
    data() {
        return {
            jsonData: {
                amf: {
                    sbi: [
                        {
                            addr: "127.0.0.5",
                            port: 7777,
                        },
                    ],
                    ngap: [
                        {
                            addr: "10.88.120.1",
                        },
                    ],
                },
                parameter: null,
                max: null,
                pool: null,
                time: null,
            },
        };
    },
    mounted() {},
    components: {
        JsonEditor,
    },
    methods: {},
};
</script>
<style lang="scss" scoped></style>

汉化问题
在这里插入图片描述
如果要将英文换成中文,需要去jsoneditor.js文件中将对应英文改成中文即可,样式自己慢慢调吧

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值