当前位置: 首页 > news >正文

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 中需要添加的配置,在这篇文章中找到了

核心代码

依赖安装

  "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

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

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

组件源码

<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

<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

http://www.lryc.cn/news/494963.html

相关文章:

  • Spark优化--开发调优、资源调优、数据倾斜调优和shuffle调优等
  • Day1 生信新手笔记
  • Python的秘密基地--[章节2]Python核心数据结构
  • 【Electron学习笔记(三)】Electron的主进程和渲染进程
  • [免费]SpringBoot+Vue景区订票(购票)系统【论文+源码+SQL脚本】
  • idea_工程与模块管理
  • A02、Java 设计模式优化
  • jdk8没有Buffer.put()
  • Artec Leo:航海设备维护的便携式3D扫描利器【沪敖3D】
  • HCIA笔记6--路由基础
  • 说说Elasticsearch拼写纠错是如何实现的?
  • Ubuntu20.04运行R-VIO2
  • 【软件项目测试文档大全】软件测试方案,验收测试计划,验收测试报告,测试用例,集成测试,测试规程和指南,等保测试(Word原件)
  • Kubernetes集群操作
  • 分布式事务调研
  • Webpack 的构建流程
  • Cesium 当前位置矩阵的获取
  • ubuntu24.04 python环境
  • YOLO系列论文综述(从YOLOv1到YOLOv11)【第9篇:YOLOv7——跨尺度特征融合】
  • Elasticearch索引mapping写入、查看、修改
  • 【大模型微调】一些观点的总结和记录
  • Vue 3 Hooks 教程
  • pandas数据处理及其数据可视化的全流程
  • docker 在ubuntu系统安装,以及常用命令,配置阿里云镜像仓库,搭建本地仓库等
  • torch.maximum函数介绍
  • Java面试之多线程并发篇(9)
  • Java全栈:超市购物系统实现
  • 1.1 数据结构的基本概念
  • 深度学习:GPT-2的MindSpore实践
  • 【Oracle11g SQL详解】ORDER BY 子句的排序规则与应用