Vue:Bin Code Editor格式化JSON编辑器

最终效果如下图所示,

Bin Code Editor安装

npm或yarn安装命令如下,

bash 复制代码
npm i bin-code-editor -S
# or 
yarn add bin-code-editor

组件注册

全局注册

在 main.js 中写入导入以下内容,

javascript 复制代码
import Vue from 'vue';
import CodeEditor from 'bin-code-editor';
import 'bin-code-editor/lib/style/index.css';

Vue.use(CodeEditor);

局部注册

在需要使用Bin Code Editor的组件中导入以下内容,

javascript 复制代码
import { CodeEditor } from 'bin-code-editor'
import 'bin-code-editor/lib/style/index.css';

export default {
    components: {
        CodeEditor
    },
}

使用方式

基本使用

value:必需,对应要转换的 JavaScript 值(通常为对象或数组),也可使用v-model代替,值的类型为String字符串,对于JavaScript对象,可以使用JSON.stringfy函数进行转换。

html 复制代码
<template>
<div>
  <b-code-editor v-model="jsonStr" :indent-unit="4" height="auto"/>
</div>
</template>
<script>
  const jsonData = `{"title":"测试json数据","children":[{"name":"子项名称", "desc":"子项说明" },{"name":"子项名称1", "desc":"子项说明1" }]}`

  export default {
    data() {
      return {
        jsonStr: jsonData
      }
    }
  }
</script>

组件属性

组件事件与方法

踩坑指南

坑点描述

修改v-model绑定值之后,需要点击一下编辑区才能显示。

坑点解决:nextTick

将修改绑定值的操作,放在nextTick内部执行,例如:

javascript 复制代码
                this.$nextTick(()=>{
                    //TODO:假设绑定值为query
                    this.query = JSON.stringify({...});//转为字符串
                });
相关推荐
We་ct2 小时前
深度剖析浏览器跨域问题
开发语言·前端·浏览器·跨域·cors·同源·浏览器跨域
weixin_427771613 小时前
前端调试隐藏元素
前端
爱上好庆祝4 小时前
学习js的第五天
前端·css·学习·html·css3·js
C澒4 小时前
IntelliPro 产研协作平台:基于 AI Agent 的低代码智能化配置方案设计与实现
前端·低代码·ai编程
一袋米扛几楼984 小时前
【Git】规范化协作:详解 GitHub 工作流中的 Issue、Branch 与 Pull Request 最佳实践
前端·git·github·issue
网络点点滴4 小时前
前端与后端的区别与联系
前端
EnCi Zheng5 小时前
M5-markconv自定义CSS样式指南 [特殊字符]
前端·css·python
kyriewen5 小时前
你的网页慢,用户不说直接走——前端性能监控教你“读心术”
前端·性能优化·监控
广州华水科技5 小时前
北斗GNSS变形监测在大坝安全监测中的应用与优势分析
前端
前端老石人5 小时前
前端开发中的 URL 完全指南
开发语言·前端·javascript·css·html