版本:"vue-json-editor": "^1.4.3"

(1)下载依赖,npm install vue-json-editoryarn add vue-json-editor

(2)使用 

<div class="code-json-editor">
    <vue-json-editor
        v-model="jsonEditorData"
        :showBtns="false" // 不显示保存按钮
        :mode="'code'" // 默认选择code格式
        lang="zh" // 中文
        :expandedOnStart="true" // 默认展开数据
        @json-change="jsonEditorDataChange" // 当输入的数据符合json对象格式时
        @has-error="jsonEditorDataHasError"/> // 当输入的数据不符合json对象格式时
    </div>
    <div v-if="showErrorTips">格式错误,请输入json格式</div>
</div>

<script>
import VueJsonEditor from 'vue-json-editor'
export default {
    components: {
        VueJsonEditor
    },
    data () {
        return {
          showErrorTips: false,
          jsonEditorData: {}
        }
    },
    methods: {
        jsonEditorDataChange (json) {
            this.jsonEditorData = json
            this.showErrorTips = false
        },
        jsonEditorDataHasError (error) {
            // 当清空输入的数据时
            if (error.message && error.message.includes("Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'EOF'")) {
                this.jsonEditorData= {}
                this.showErrorTips = false
            } else {
                this.showErrorTips = true
            }
        }
    }
}
</script>

// 隐藏 右上角链接
<style scoped="scoped">
    .code-json-editor >>> .jsoneditor-poweredBy {
        display: none !important;
    }
</style>

Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐