vue-cli整合monaco代码编辑器
Monaco代码编辑器
微软官方编辑器:https://github.com/microsoft/monaco-editor
官方提供的整合教程:https://github.com/microsoft/monaco-editor/blob/main/docs/integrate-esm.md
- 该开源代码编辑器是微软官方编辑器,操作方式和风格都很想vscode
- 可以扩展性强
- 相比较于md编辑器更适合写编程语言的编写,md更适合编程文章
安装
安装编辑器
monaco-editor- ```sh
npm install monaco-editor —save1
2
3
4
5
6
+ vue-cli 项目(webpack 项目)整合 monaco-editor。
先安装 monaco-editor-webpack-plugin(https://github.com/microsoft/monaco-editor/blob/main/webpack-plugin/README.md):
+ ```sh
npm install monaco-editor-webpack-plugin --save
- ```sh
配置
如何使用 Monaco Editor?查看示例教程:
https://microsoft.github.io/monaco-editor/playground.html?source=v0.40.0#example-creating-the-editor-hello-world在 vue.config.js 中配置 webpack 插件:
全量加载:
1 | const { defineConfig } = require("@vue/cli-service"); |
按需加载:
1 | const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin') |
创建Vue组件
注意,monaco editor 在读写值的时候,要使用 toRaw(编辑器实例)的语法来执行操作,否则会卡死。
示例整合代码如下:
1 | <template> |
实例化代码及具体配置如下:
1 | //const beautify = require('js-beautify') |
本地化 i18n
本地化采用monaco-editor-esm-webpack-plugin方式实现,Github地址:https://github.com/wang12124468/monaco-editor-esm-webpack-plugin 。
1 | # 汉化扩展(需要下载包的): |
1 | const { defineConfig } = require("@vue/cli-service"); |
评论




