vue3使用monaco-editor组件

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

在这里插入图片描述

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>

Vue 3使用monaco-editor(微软的代码编辑器)可以通过以下步骤实现: 1. **安装monaco-editor**: 首先,使用npm或yarn安装monaco-editor。 ```bash npm install monaco-editor # 或者 yarn add monaco-editor ``` 2. **创建编辑器组件**: 创建一个新的Vue组件,例如`MonacoEditor.vue`,并在其中引入monaco-editor。 ```vue <template> <div ref="editorContainer" style="height: 500px;"></div> </template> <script> import { onMounted, ref, watch } from &#39;vue&#39;; import * as monaco from &#39;monaco-editor&#39;; export default { name: &#39;MonacoEditor&#39;, props: { modelValue: { type: String, default: &#39;&#39; }, language: { type: String, default: &#39;javascript&#39; } }, setup(props, { emit }) { const editorContainer = ref(null); let editor = null; const initEditor = () => { editor = monaco.editor.create(editorContainer.value, { value: props.modelValue, language: props.language, automaticLayout: true }); editor.onDidChangeModelContent(() => { emit(&#39;update:modelValue&#39;, editor.getValue()); }); }; onMounted(() => { initEditor(); }); watch(() => props.modelValue, (newValue) => { if (editor) { editor.setValue(newValue); } }); return { editorContainer }; } }; </script> <style scoped> /* 样式可以根据需要自行调整 */ </style> ``` 3. **使用编辑器组件**: 在需要使用编辑器的父组件中引入并使用`MonacoEditor`组件。 ```vue <template> <div> <MonacoEditor v-model="code" language="javascript" /> </div> </template> <script> import MonacoEditor from &#39;./components/MonacoEditor.vue&#39;; export default { components: { MonacoEditor }, data() { return { code: &#39;console.log("Hello, Monaco Editor!");&#39; }; } }; </script> ``` 4. **按需引入(可选)**: 为了减小打包体积,可以使用按需引入的方式引入monaco-editor。 ```javascript // 在入口文件或相应的配置文件中 import Editor from &#39;monaco-editor/esm/vs/editor/editor.main.js&#39;; Editor.create(document.getElementById(&#39;container&#39;), { value: &#39;console.log("Hello, Monaco Editor!");&#39;, language: &#39;javascript&#39; }); ``` 通过以上步骤,你就可以在Vue 3项目中使用monaco-editor了。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值