vue-json-editor json編輯器的使用
一、概述
現(xiàn)有一個(gè)vue項(xiàng)目,需要一個(gè)json編輯器,能夠格式化json數(shù)據(jù),同時(shí)也支持編輯功能。
vue-json-editor 插件就可以實(shí)現(xiàn)這個(gè)功能
二、vue-json-editor 使用
安裝插件
npm install vue-json-editor --save
使用
test.vue
<template> <div style="width: 70%;margin-left: 30px;margin-top: 30px;"> <vue-json-editor v-model="resultInfo" :showBtns="false" :mode="'code'" @json-change="onJsonChange" @json-save="onJsonSave" @has-error="onError" /> <br> <el-button type="primary" @click="checkJson">確定</el-button> </div> </template> <script> // 導(dǎo)入模塊 import vueJsonEditor from 'vue-json-editor' export default { // 注冊(cè)組件 components: { vueJsonEditor }, data() { return { hasJsonFlag:true, // json是否驗(yàn)證通過(guò) // json數(shù)據(jù) resultInfo: { 'employees': [ { 'firstName': 'Bill', 'lastName': 'Gates' }, { 'firstName': 'George', 'lastName': 'Bush' }, { 'firstName': 'Thomas', 'lastName': 'Carter' } ] } } }, mounted: function() { }, methods: { onJsonChange (value) { // console.log('更改value:', value); // 實(shí)時(shí)保存 this.onJsonSave(value) }, onJsonSave (value) { // console.log('保存value:', value); this.resultInfo = value this.hasJsonFlag = true }, onError(value) { // console.log("json錯(cuò)誤了value:", value); this.hasJsonFlag = false }, // 檢查json checkJson(){ if (this.hasJsonFlag == false){ // console.log("json驗(yàn)證失敗") // this.$message.error("json驗(yàn)證失敗") alert("json驗(yàn)證失敗") return false } else { // console.log("json驗(yàn)證成功") // this.$message.success("json驗(yàn)證成功") alert("json驗(yàn)證成功") return true } }, } } </script> <style> </style>
插件參數(shù)說(shuō)明:
<vue-json-editor v-model="resultInfo" // 綁定數(shù)據(jù)resultInfo :showBtns="false" // 是否顯示保存按鈕 :mode="'code'" // 默認(rèn)編輯模式 // 顯示中文,默認(rèn)英文 @json-change="onJsonChange" // 數(shù)據(jù)改變事件 @json-save="onJsonSave" // 數(shù)據(jù)保存事件 @has-error="onError" // 數(shù)據(jù)錯(cuò)誤事件 />
相關(guān)說(shuō)明:
resultInfo 默認(rèn)綁定的變量,這個(gè)變量可以為空,編輯器會(huì)顯示為{}
:showBtns 這里不顯示保存按鈕,為什么呢?原因有2個(gè)。1. 默認(rèn)樣式不好看。2. 只能當(dāng)json數(shù)據(jù)正確,才能點(diǎn)擊保存按鈕,否則禁止點(diǎn)擊。
json-change,json-save,has-error 這3個(gè)事件,是會(huì)實(shí)時(shí)觸發(fā)的。
這里我額外加了一個(gè)檢測(cè)方法,用來(lái)判斷json數(shù)據(jù)是否正確。默認(rèn)標(biāo)記為true,當(dāng)不正確時(shí),會(huì)改變狀態(tài)為false。
訪問(wèn)
點(diǎn)擊確定,提示成功
改為錯(cuò)誤的,點(diǎn)擊確定,會(huì)提示失敗。
注意:這個(gè)json編輯會(huì)帶有下來(lái)菜單,實(shí)際項(xiàng)目中,需要去除,比較用戶誤操作。
在實(shí)際使用中發(fā)現(xiàn)幾個(gè)問(wèn)題:
1. 輸入中文時(shí),傳給后端的值不多
2. 輸入大量json時(shí),會(huì)有部分?jǐn)?shù)據(jù)丟失。
因此,我們使用下面的編輯器bin-code-editor
三、bin-code-editor
安裝模塊
npm install bin-code-editor -d
引入
在 main.js 中寫(xiě)入2行
import CodeEditor from 'bin-code-editor'; Vue.use(CodeEditor);
test.vue
<template> <div style="width: 70%;margin-left: 30px;margin-top: 30px;"> <b-code-editor v-model="jsonStr" :auto-format="true" :smart-indent="true" theme="dracula" :indent-unit="4" :line-wrap="false" ref="editor"></b-code-editor> <br> <el-button type="primary" @click="onSubumit">提交</el-button> </div> </template> <script> const jsonData =`{ "employees": [{ "firstName": "Bill", "lastName": "Gates" }, { "firstName": "George", "lastName": "Bush" }, { "firstName": "Thomas", "lastName": "Carter" }] }` export default { data() { return { jsonStr:jsonData } }, methods: { // 檢測(cè)json格式 isJSON(str) { if (typeof str == 'string') { try { var obj=JSON.parse(str); if(typeof obj == 'object' && obj ){ return true; }else{ return false; } } catch(e) { return false; } }else if (typeof str == 'object' && str) { return true; } }, onSubumit(){ if (!this.isJSON(this.jsonStr)){ this.$message.error(`json格式錯(cuò)誤`) return false } this.$message.success('json格式正確') } } } </script> <style> </style>
訪問(wèn)測(cè)試頁(yè)面,效果如下:
輸入錯(cuò)誤的值,點(diǎn)擊執(zhí)行,會(huì)有提示
到此這篇關(guān)于vue-json-editor json編輯器的使用的文章就介紹到這了,更多相關(guān)vue json編輯器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue 1.x 交互實(shí)現(xiàn)仿百度下拉列表示例
本篇文章主要介紹了vue 1.x 交互實(shí)現(xiàn)仿百度下拉列表示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10vue中使用element ui的彈窗與echarts之間的問(wèn)題詳解
這篇文章主要介紹了vue中使用element ui的彈窗與echarts之間的問(wèn)題詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10vue實(shí)現(xiàn)消息向上無(wú)縫滾動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)消息向上無(wú)縫滾動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04Vue?Input輸入框自動(dòng)獲得焦點(diǎn)的有效方法
我們有時(shí)候會(huì)遇到要輸入框自動(dòng)獲取焦點(diǎn)的情況,下面這篇文章主要給大家介紹了關(guān)于Vue?Input輸入框自動(dòng)獲得焦點(diǎn)的簡(jiǎn)單方法,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11SpringBoot+Vue3實(shí)現(xiàn)文件的上傳和下載功能
上傳文件和下載文件是我們平時(shí)經(jīng)常用到的功能,接下來(lái)就讓我們用SpringBoot,Vue3和ElementPlus組件實(shí)現(xiàn)文件的上傳和下載功能吧,感興趣的朋友跟隨小編一起看看吧2023-01-01