解决精度丢失问题,移步我的其他文章
vue-json-editor
1.安装vue-json-editor
npm install vue-json-editor --save
2.在.vue 文件中引用
import JsonEditor from "vue-json-editor";
3.使用
<template>
<div class = "routeManagement">
<vue-json-editor
v-model="routeJson" :showBtns="true" lang="zh"
@json-change="onJsonChange" @json-save = "onJsonSave"/>
</div>
</template>
<script>
// 引入vue-json-editor模块
import vueJsonEditor from 'vue-json-editor'
export default {
data () {
return{
// 可使用 JSON.parse() JSON.stringify() 转化 json 数据
json: {
msg: 'demo of jsoneditor'
}
}
},
components: {
vueJsonEditor
},
methods: {
onJsonChange () { // 数据改变时触发
},
onJsonSave(){ // 点击保存触发
}
},
}
</script>
<style lang="scss" scoped>
.routeManagement{
width:98%;
margin:16px auto;
/deep/.jsoneditor-vue{
height:700px;
}
/deep/.json-save-btn{
cursor: pointer;
}
}
</style>
4.效果
codemirror + json-lint
1.安装
npm install codemirror
npm install json-lint
2.组件
<template>
<div class="json-editor">
<textarea ref="textarea" />
</div>
</template>
<script>
import CodeMirror from 'codemirror'
import 'codemirror/addon/lint/lint.css'
import 'codemirror/lib/codemirror.css'
import 'codemirror/theme/rubyblue.css'
require('script-loader!jsonlint')
import 'codemirror/mode/javascript/javascript'
import 'codemirror/addon/lint/lint'
import 'codemirror/addon/lint/json-lint'
export default {
name: 'JsonEditor',
/* eslint-disable vue/require-prop-types */
props: ['value'],
data() {
return {
jsonEditor: false
}
},
watch: {
value(value) {
const editorValue = this.jsonEditor.getValue()
if (value !== editorValue) {
this.jsonEditor.setValue(JSON.stringify(this.value, null, 2))
}
}
},
mounted() {
this.jsonEditor = CodeMirror.fromTextArea(this.$refs.textarea, {
lineNumbers: true,
mode: 'application/json',
gutters: ['CodeMirror-lint-markers'],
theme: 'rubyblue',
lint: true
})
this.jsonEditor.setValue(JSON.stringify(this.value, null, 2))
this.jsonEditor.on('change', cm => {
this.$emit('changed', cm.getValue())
this.$emit('input', cm.getValue())
})
},
methods: {
getValue() {
return this.jsonEditor.getValue()
}
}
}
</script>
<style lang="scss" scoped>
.json-editor {
height: 100%;
position: relative;
::v-deep {
.CodeMirror {
height: auto;
min-height: 300px;
}
.CodeMirror-scroll {
min-height: 300px;
}
.cm-s-rubyblue span.cm-string {
color: #F08047;
}
}
}
</style>
3.使用
先引入组件然后直接用
<JsonEditor ref="jsonEditor" v-model="jsonValue"></JsonEditor>
4.效果图
版权声明:本文为shaoxxxxx原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。