前端可视的json编辑与回显,bin-code-editor

项目中有个需求,是调试接口,需要在前端写入参
本来写的输入框,但是感觉太low了,后面做了json输入
在这里插入图片描述

第一步

安装引入插件

npm i bin-code-editor
import CodeEditor from 'bin-code-editor'
Vue.use(CodeEditor)

第二步

使用

	// 入参
   <b-code-editor
      ref="editor"
      v-model="form.ext"
      :auto-format="true"
      :smart-indent="true"
      theme="idea"
      :indent-unit="4"
      :line-wrap="false"
      :show-number="true"
     />
    <el-button size="mini" type="primary" @click="handleFormat">格式化校验</el-button>
	

	// 返回参数
   <b-code-editor
     ref="resopnse"
     v-model="form.resopnse"
      :auto-format="true"
      :smart-indent="true"
      readonly
      theme="idea"
      :indent-unit="4"
      :line-wrap="false"
      :show-number="true"
     />


	// 判断是不是json
    isValidJSON(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
        }
      }
    },
    // 点击格式化按钮
    handleFormat() {
      this.$refs.editor.formatCode()
      setTimeout(() => {
        const v = this.$refs.editor.getValue()
        console.log('获取的输入框内容的数据:', v)
        if (this.isValidJSON(v)) {
          console.log('JSON.parse后的数据:', JSON.parse(v))
        } else {
          console.log(JSON.stringify(v))
        }
      }, 1000)
    },
    调用接口并回显内容
    save () {
    	// ...这里省去接口调用 res是接口返回的数据
        this.form.resopnse = JSON.stringify(res)
        this.$nextTick(() => {
          this.$refs.resopnse.formatCode()
        })
    }

属性
在这里插入图片描述
theme属性可选值
idea、eclipse、duotone-light、mdn-like、xq-light、dracula、rubyblue、monokai、material、material-darker

方法
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值