实现效果如下(切换语言、获取数据、格式化代码、设置默认数据)

在这里插入图片描述

1. 安装monaco-editor

npm install monaco-editor@0.33.0 --save
npm install --save-dev vite-plugin-monaco-editor@1.0.10 ( 必须安装)

2. vite.config.js配置

import monacoEditorPlugin from "vite-plugin-monaco-editor"
 plugins: [
    // monacoEditorPlugin() 不能不写{}配置项 会报 Cannot read properties of undefined (reading 'languageWorkers')这个错
    monacoEditorPlugin({ languages: ['javascript', 'typescript', 'html', 'css', 'json','java','sql','groovy','shell','python']}),   
  ],

3. 页面使用

<template>
  <div>
    <a-select style="width: 120px" @change="handelChange">
      <a-select-option v-for="(item,index) in languageList" :key="index" :value="item">
        {{ item }}
      </a-select-option>
    </a-select>
    <button @click="handleBtn">获取</button>
    <button @click="handleForrmat">格式化</button>
    <button @click="handelSet">设置值</button>
    <div ref="main" style="width: 100%; height: 600px"></div>
  </div>
</template>

<script setup>
import * as monaco from "monaco-editor";
import { toRaw, onMounted, ref } from "vue";
const monacoEditor = ref(undefined);
const main = ref(null);
let languageList=ref([
  'javascript', 'json', 'sql', 'java', 'groovy', 'shell', 'python'
])
onMounted(() => {
  init();
});
const handleForrmat = () => {
  monacoEditor.value.getAction('editor.action.formatDocument').run()
};
const handleBtn = () => {
  let demo = toRaw(monacoEditor.value).getValue(); //获取编辑器中的文本
  console.log(demo)
};
const handelChange = (val) => {
  monaco.editor.setModelLanguage(monacoEditor.value.getModel(), val)
}
const handelSet=()=>{
  toRaw(monacoEditor.value).setModel(monaco.editor.createModel(`{"name":'123'}`,'json'))
}
const init = () => {
  // 使用 - 创建 monacoEditor 对象
  monacoEditor.value = monaco.editor.create(main.value, {
    theme: "hc-black", // 主题
    language: "json",
    renderLineHighlight: "gutter",
    folding: true, // 是否折叠
    roundedSelection: false,
    foldingHighlight: true, // 折叠等高线
    foldingStrategy: "indentation", // 折叠方式  auto | indentation
    showFoldingControls: "always", // 是否一直显示折叠 always | mouseover
    disableLayerHinting: true, // 等宽优化
    emptySelectionClipboard: false, // 空选择剪切板
    selectionClipboard: false, // 选择剪切板
    automaticLayout: true, // 自动布局
    codeLens: true, // 代码镜头
    scrollBeyondLastLine: false, // 滚动完最后一行后再滚动一屏幕
    colorDecorators: true, // 颜色装饰器
    accessibilitySupport: "on", // 辅助功能支持  "auto" | "off" | "on"
    lineNumbers: "on", // 行号 取值: "on" | "off" | "relative" | "interval" | function
    lineNumbersMinChars: 5, // 行号最小字符   number
    enableSplitViewResizing: false,
    readOnly: false, //是否只读  取值 true | false
  });
};
</script>

Logo

前往低代码交流专区

更多推荐