跳到主要内容

Code Editor — Vue

@aiao/code-editor-vue 提供 Vue 3 SFC 组件,支持 v-model:value 双向绑定。

安装

npm install @aiao/code-editor-vue

Peer dependencies: vue >= 3.5

基础用法

<script setup lang="ts">
import { ref } from 'vue';
import CodeEditor from '@aiao/code-editor-vue';

const code = ref('SELECT * FROM users;');
</script>

<template>
<CodeEditor v-model:value="code" language="sql" theme="dark" line-wrapping />
</template>

Props

Prop类型默认值说明
valuestring''编辑器内容
theme'light' | 'dark''light'主题
languagestring'sql'语言名称
placeholderstring''占位文本
readonlybooleanfalse只读模式
disabledbooleanfalse禁用编辑
setup'basic' | 'minimal''basic'预设扩展包
indentWithTabbooleanfalseTab 缩进
indentUnitstring' '缩进单位(默认两空格)
lineWrappingbooleanfalse自动换行
highlightWhitespacebooleanfalse高亮空白字符

Events

事件载荷说明
update:valuestringv-model:value 更新
changestring内容变化

v-model 双向绑定

<script setup lang="ts">
import { ref } from 'vue';
import CodeEditor from '@aiao/code-editor-vue';

const code = ref('');
</script>

<template>
<CodeEditor v-model:value="code" language="typescript" />
<p>字符数: {{ code.length }}</p>
</template>

并排编辑器

<script setup lang="ts">
import { ref } from 'vue';
import CodeEditor from '@aiao/code-editor-vue';

const sql = ref('SELECT * FROM users;');
const result = ref('');

async function runQuery() {
const res = await executeQuery(sql.value);
result.value = JSON.stringify(res, null, 2);
}
</script>

<template>
<div class="flex gap-4">
<CodeEditor v-model:value="sql" language="sql" class="flex-1" />
<CodeEditor :value="result" language="json" readonly class="flex-1" />
</div>
<button @click="runQuery">执行</button>
</template>

Vue 特有行为

  • readonly + disabled 合并:两者 OR 运算后同时设置 CodeMirror 的 readOnlyeditable
  • 语言匹配:使用 LanguageDescription.matchLanguageName() 静态方法
  • indentUnit 默认值' '(两空格),其他框架为空字符串