前端编辑器JSON HTML等,vue2-ace-editor,vue3-ace-editor

与框架无关

vue2-ace-editor有问题,ace拿不到(brace)

一些组件都是基于ace-builds或者brace包装的

不如直接用下面的,不如直接使用下面的

javascript 复制代码
<template>
  <div ref="editor" class="json-editor"></div>
</template>

<script>
import ace from 'ace-builds';
import 'ace-builds/webpack-resolver'; // 确保 Webpack 正确解析模块
import 'ace-builds/src-noconflict/mode-json'; // 引入 JSON 模式
import 'ace-builds/src-noconflict/theme-monokai'; // 引入 Monokai 主题

export default {
  name: 'JsonEditor',
  props: {
    value: {
      type: String,
      default: '',
    },
  },
  data() {
    return {
      editor: null,
    };
  },
  watch: {
    value(newValue) {
      if (newValue !== this.editor.getValue()) {
        this.editor.setValue(newValue, 1); // 1 表示不触发 change 事件
      }
    },
  },
  mounted() {
    this.initEditor();
  },
  beforeDestroy() {
    if (this.editor) {
      this.editor.destroy();
      this.editor = null;
    }
  },
  methods: {
    initEditor() {
      this.editor = ace.edit(this.$refs.editor, {
        mode: 'ace/mode/json',
        theme: 'ace/theme/monokai',
        tabSize: 2,
        useWorker: false, // 禁用 worker 以避免 JSON 解析错误时的警告
        minLines: 10,
        maxLines: 30,
        fontSize: '14px',
        showPrintMargin: false,
      });

      this.editor.setValue(this.value, 1); // 初始化值

      this.editor.getSession().on('change', () => {
        this.$emit('input', this.editor.getValue());
      });
    },
  },
};
</script>

<style scoped>
.json-editor {
  width: 100%;
  height: 400px;
}
</style>

解决光标错位

css 复制代码
.ace_editor,
.ace_editor * {
  font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', 'Droid Sans Mono', 'Consolas', monospace !important;
  font-size: 12px !important;
  font-weight: 400 !important;
  letter-spacing: 0 !important;
}
相关推荐
阿赛工作室3 分钟前
AI时代WEB开发人员生存与发展报告
前端·人工智能·node.js
ZC跨境爬虫19 分钟前
跟着 MDN 学 HTML day_36:(深入理解 Comment 接口与 DOM 注释节点)
前端·javascript·ui·html·音视频·视频编解码
石小石Orz38 分钟前
Harness Engineering 到底是什么?概念、实战与争议,一次全部讲清楚
前端·后端
悠哉摸鱼大王43 分钟前
cesium学习(三)-3d tiles
前端·cesium
前端那点事44 分钟前
Vue3自定义Hooks保姆级教程!从原理到企业级实战,告别混乱代码
前端·vue.js
前端那点事1 小时前
别再乱用Vue3响应式!ref、reactive、toRef、toRefs完整区别+企业级落地实战
前端·vue.js
yingyima1 小时前
Base64 编码解码实战:业务场景下的高效应用
前端
悠哉摸鱼大王1 小时前
cesium学习(五)-Primitive
前端·cesium
悟空瞎说1 小时前
Git Worktree 实战:多 AI 编码代理并行开发,彻底解决分支切换冲突痛点
前端·git
悠哉摸鱼大王1 小时前
cesium学习(四)-相机
前端·cesium