最近开发中使用到了脚本编辑器vue2-ace-editor,这里介绍一下其简单的用法
一、安装
npm install vue2-ace-editor --save
二、简单组件封装
<div class="editor-box">
<ace
ref="editor"
:value="content"
@init="initEditor"
:lang="lang"
:height="height === 0 ? '100%' : height"
:theme="theme"
:options="options"
width="100%"
v-bind="config">
</ace>
</div>
<script>
import ace from 'vue2-ace-editor'
export default {
name: 'SqlEditor',
components: {
ace
},
props: {
content: {
type: String,
default: ''
},
height: {
type: Number,
default: 0
},
readOnly: {
type: Boolean,
default: false
},
theme: {
type: String,
default: 'monokai'
},
lang: {
type: String,
default: 'sql'
},
config: {
type: Object,
default: () => {
return {
font_size: 16,
sql_atom: true
}
}
}
},
computed: {
options () {
if (this.readOnly) {
return {
enableBasicAutocompletion: true,
enableSnippets: true,
enableLiveAutocompletion: this.config.sql_atom,
showPrintMargin: false,
fontSize: this.config.font_size,
readOnly: true
}
}
return {
enableBasicAutocompletion: true,
enableSnippets: true,
enableLiveAutocompletion: this.config.sql_atom,
showPrintMargin: false,
fontSize: this.config.font_size
}
}
},
created () {
},
methods: {
initEditor (editor) {
require('brace/ext/language_tools')
require('brace/mode/sql')
require('brace/snippets/sql')
require('brace/theme/monokai')
require('brace/theme/chrome')
require('brace/theme/crimson_editor')
editor.getSession().on('change', val => {
this.$emit('change', editor.getValue())
})
}
}
}
</script>
三、组件使用
import Editor from '@/components/common/Editor.vue'
<editor
ref="editors"
:content="value"
:theme="'crimson_editor'"
:config="config"
@change="editorChange"></editor>
npm install sql-formatter
import { format } from 'sql-formatter'
formatter () {
const editor = this.$refs.editor.editor
const content = editor.getValue()
const formatContent = format(content)
editor.setValue(formatContent)
}
扩展,ace的基础用法和属性

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」