vue3使用monaco编辑器(VSCode网页版)

vue3使用monaco编辑器(VSCode网页版)

文章说明

一直在找网页版的编辑器,网页版的VSCode功能很强大,这个monaco就是VSCode样式的编辑器,功能很强大,可以直接使用;它的版本有变更,导致可能vue3使用会报一些小错误,我这里书写一个示例,来简单使用该组件,主要解决 babel.cosfig.js 和 vue.config.js 内缺少配置导致的报错

参考文章

Vue3 集成Monaco Editor编辑器

使用的demo代码是从这里复制过来的,不同的是,这里稍微补充了一下vue3相关的配置,不然会运行报错
Vue3中使用Monaco Editor代码编辑器

这篇文章中提到了 vue.config.js 的配置
vue报错之" Static class blocks are not enabled. Please add `@babel/plugin-transform-class-static-block"

最后一个 babel.config.js 中需要添加的配置,在这篇文章中找到了

核心代码

依赖安装

xml 复制代码
  "dependencies": {
    "core-js": "^3.8.3",
    "monaco-editor": "^0.52.0",
    "monaco-editor-webpack-plugin": "^7.1.0",
    "vue": "^3.2.13"
  },

vue.config.js

javascript 复制代码
const {defineConfig} = require('@vue/cli-service')
const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin')

module.exports = defineConfig({
    transpileDependencies: true,
    lintOnSave: false,
    publicPath: './',
    configureWebpack: {
        plugins: [
            new MonacoWebpackPlugin()
        ]
    }
})

babel.config.js

javascript 复制代码
module.exports = {
    presets: [
        '@vue/cli-plugin-babel/preset'
    ],
    plugins: ["@babel/plugin-transform-private-methods", "@babel/plugin-transform-class-static-block"],
}

组件源码

html 复制代码
<script setup>
import * as monaco from 'monaco-editor';
import {onMounted, onUnmounted, ref} from 'vue'

const editorContainer = ref();
const editor = ref();

onMounted(() => {
  if (editorContainer.value) {
    editor.value = monaco.editor.create(editorContainer.value, {
      value: "",
      language: 'javascript',
      theme: 'vs-dark',
      codeLens: true,
      folding: true,
      snippetSuggestions: 'inline',
      tabCompletion: 'on',
      foldingStrategy: 'auto',
      smoothScrolling: true,
    });

    editor.value.onDidChangeModelContent(() => {
      console.log('编辑器内容变更')
    })
  }
});

onUnmounted(() => {
  if (editor.value) {
    editor.value.dispose();
  }
});
</script>

<template>
  <div ref="editorContainer" class="container"></div>
</template>

<style scoped>
.container {
  width: 100%;
  height: 100%;
}
</style>

App.vue

html 复制代码
<script setup>
import Editor from "@/Editor.vue";
</script>

<template>
  <div style="width: 100vw; height: 100vh">
    <Editor/>
  </div>
</template>

<style>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
</style>

效果展示

编辑JavaScript代码

实践说明

在运行过程中会提示这个文件大小超过500KB,不过不影响运行

源码下载

monaco使用demo

相关推荐
咬人喵喵21 小时前
文生图:AI 是怎么把文字变成画的?
人工智能·编辑器·svg
啃火龙果的兔子21 小时前
目前免费的ai编辑器或者vscode适用的免费的ai插件有哪些
人工智能·vscode·编辑器
小桥流水人家丶21 小时前
vscode 格式Prettier配置
ide·vscode·编辑器
番茄灭世神1 天前
使用VScode开发ARM核芯片通用配置
arm开发·vscode·mcu·cmake·clangd·llvm·ninja
攻城狮之路人甲1 天前
用pycharm写的程序,点击.py无法运行闪退
ide·python·pycharm
Aevget1 天前
Python开发利器PyCharm v2025.3全新发布——支持主动数据探索
开发语言·ide·python·pycharm
睡觉待开机1 天前
vscode+gitee+picgo实现稳定图床教程
ide·vscode·gitee
啃火龙果的兔子1 天前
vscode中的Gemini CLI Launcher插件作用
ide·vscode·编辑器
咬人喵喵1 天前
JavaScript 变量:let 和 const 该用谁?
前端·css·编辑器·交互·svg
石油人单挑所有1 天前
VsCode无法与远端服务器建立连接的解决方案
服务器·ide·vscode