vue-json-editor json編輯器的使用
一、概述
現(xiàn)有一個vue項(xiàng)目,需要一個json編輯器,能夠格式化json數(shù)據(jù),同時也支持編輯功能。
vue-json-editor 插件就可以實(shí)現(xiàn)這個功能
二、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 {
// 注冊組件
components: { vueJsonEditor },
data() {
return {
hasJsonFlag:true, // json是否驗(yàn)證通過
// 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í)時保存
this.onJsonSave(value)
},
onJsonSave (value) {
// console.log('保存value:', value);
this.resultInfo = value
this.hasJsonFlag = true
},
onError(value) {
// console.log("json錯誤了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ù)說明:
<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ù)錯誤事件
/>相關(guān)說明:
resultInfo 默認(rèn)綁定的變量,這個變量可以為空,編輯器會顯示為{}
:showBtns 這里不顯示保存按鈕,為什么呢?原因有2個。1. 默認(rèn)樣式不好看。2. 只能當(dāng)json數(shù)據(jù)正確,才能點(diǎn)擊保存按鈕,否則禁止點(diǎn)擊。
json-change,json-save,has-error 這3個事件,是會實(shí)時觸發(fā)的。
這里我額外加了一個檢測方法,用來判斷json數(shù)據(jù)是否正確。默認(rèn)標(biāo)記為true,當(dāng)不正確時,會改變狀態(tài)為false。
訪問
點(diǎn)擊確定,提示成功

改為錯誤的,點(diǎn)擊確定,會提示失敗。

注意:這個json編輯會帶有下來菜單,實(shí)際項(xiàng)目中,需要去除,比較用戶誤操作。
在實(shí)際使用中發(fā)現(xiàn)幾個問題:
1. 輸入中文時,傳給后端的值不多
2. 輸入大量json時,會有部分?jǐn)?shù)據(jù)丟失。
因此,我們使用下面的編輯器bin-code-editor
三、bin-code-editor
安裝模塊
npm install bin-code-editor -d
引入
在 main.js 中寫入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: {
// 檢測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格式錯誤`)
return false
}
this.$message.success('json格式正確')
}
}
}
</script>
<style>
</style>訪問測試頁面,效果如下:

輸入錯誤的值,點(diǎn)擊執(zhí)行,會有提示

到此這篇關(guān)于vue-json-editor json編輯器的使用的文章就介紹到這了,更多相關(guān)vue json編輯器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue 1.x 交互實(shí)現(xiàn)仿百度下拉列表示例
本篇文章主要介紹了vue 1.x 交互實(shí)現(xiàn)仿百度下拉列表示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10
vue中使用element ui的彈窗與echarts之間的問題詳解
這篇文章主要介紹了vue中使用element ui的彈窗與echarts之間的問題詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
SpringBoot+Vue3實(shí)現(xiàn)文件的上傳和下載功能
上傳文件和下載文件是我們平時經(jīng)常用到的功能,接下來就讓我們用SpringBoot,Vue3和ElementPlus組件實(shí)現(xiàn)文件的上傳和下載功能吧,感興趣的朋友跟隨小編一起看看吧2023-01-01

