vue中使用 json编辑器

<template>
  <div class="stringTest">
        <vue-json-editor
          v-model="vstringData"    //编辑器中的内容
          :showBtns="false"        // 保存按钮
          mode="code"
          lang="zh"
          :expanded-on-start="true"
          @json-change="onJsonChange"  //改变内容时触发
          @json-save="onJsonSave"      //点击保存按钮
          @has-error="onError"         //校验json内容
        />
  </div>
</template>

<script>

export default {
  components: {  },
  data() {
    return {
      hasJsonFlag: true, // json是否验证通过
      vstringData: {
        //这个变量可以为空,编辑器会显示为{}
        style: {
          width: "300px",
          height: "30px",
          display: "flex",
        },
        data: {
          name: "任务名称",
          title: "",
          help: "请输入字符串类型的内容",
          default: "",
          widget: {},
          reg: {},
          isLoad: false,
          readonly: false,
        },
      },
    };
  },
  mounted() {
   
  },
  methods: {
    onJsonChange(value) {
      // 实时保存
      this.onJsonSave(value);
    },
    onJsonSave(value) {
      this.vstringData = value;
      this.hasJsonFlag = true;
    },
    onError(value) {
      this.$message.error(`json错误了${value}`);
      this.hasJsonFlag = false;
    },
    // 检查json
    checkJson() {
      if (this.hasJsonFlag === false) {
        this.$message.error("请输入格式正确的JSON数据!");
        return false;
      } else {
        return true;
      }
    },
  },
};
</script>

<style lang="less" scoped>
.stringTest {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  padding: 20px;
  background: lightyellow;

  /* jsoneditor右上角默认有一个链接,加css去掉 */
  /deep/ .jsoneditor-poweredBy {
    display: none !important;
  }

  /*修改高度*/
  /deep/ .jsoneditor-outer {
    height: 500px;
    margin-top: 10px;
  }
  /*修改菜单栏背景颜色,原始背景为蓝色,为了和整体页面风格一致,改为黑色*/
  /deep/ .jsoneditor-menu {
    background-color: #000;
    border-bottom: 1px solid #000;
  }

  /*修改输入框边框颜色*/
  /deep/.jsoneditor {
    border: 1px solid #000;
  }
}
</style>

 大概效果的如下:

相关推荐

  1. vue-codeirror编辑器vue3使用

    2024-07-10 14:04:04       19 阅读
  2. Vue使用markdown编辑器

    2024-07-10 14:04:04       37 阅读
  3. Vue3使用WangEditor编辑器

    2024-07-10 14:04:04       43 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-10 14:04:04       4 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-10 14:04:04       5 阅读
  3. 在Django里面运行非项目文件

    2024-07-10 14:04:04       4 阅读
  4. Python语言-面向对象

    2024-07-10 14:04:04       5 阅读

热门阅读

  1. 大数据面试题之Greenplum(2)

    2024-07-10 14:04:04       7 阅读
  2. 准备GPU H20机器k8s环境时用到的链接

    2024-07-10 14:04:04       8 阅读
  3. 数据库的优点和缺点分别是什么

    2024-07-10 14:04:04       10 阅读
  4. SQL语句分类

    2024-07-10 14:04:04       10 阅读
  5. vue安装总是失败原因剖析

    2024-07-10 14:04:04       12 阅读
  6. 使用Spring Cloud构建微服务架构下的淘客返利系统

    2024-07-10 14:04:04       8 阅读