vue3使用monaco-editor
第一步
npm i monaco-editor@0.34.1 monaco-editor-webpack-plugin@7.0.1
第二步
// 随便找一个页面里都可以
<template>
<!-- <button @click="format">格式化</button> -->
<button @click="getCodeContext">获取数据</button>
<button @click="handleTheme">设置主题</button>
<div id="app" ref="editorContainer"></div>
</template>
<script setup lang="ts">
import { onMounted, ref, toRaw } from "vue";
import * as monaco from "monaco-editor";
const codeContent = ref("");
const editorContainer = ref<any>(null);
const editor = ref<any>(null);
const editorTheme = ref<string>("vs-dark");
onMounted(() => {
editor.value = monaco.editor.create(editorContainer.value, {
value: "",
theme: editorTheme.value, // 主题
language: "javascript",
folding: true, // 是否折叠
foldingHighlight: true, // 折叠等高线
foldingStrategy: "indentation", // 折叠方式 auto | indentation
showFoldingControls: "always", // 是否一直显示折叠 always | mouSEO((Search Engine Optimization))ver
disableLayerHinting: true, // 等宽优化
emptySelectionClipboard: false, // 空选择剪切板
selectionClipboard: false, // 选择剪切板
automaticLayout: true, // 自动布局
codeLens: false, // 代码镜头
scrollBeyondLastLine: false, // 滚动完最后一行后再滚动一屏幕
colorDecorators: true, // 颜色装饰器
accessibilitySupport: "off", // 辅助功能支持 "auto" | "off" | "on"
lineNumbers: "on", // 行号 取值: "on" | "off" | "relative" | "interval" | function
lineNumbersMinChars: 5, // 行号最小字符 number
readOnly: false, //是否只读 取值 true | false
});
// 监听内容变化
editor.value.onDidChangeModelContent((e) => {});
console.log(editor.value, "监听内容变化");
// 监听失去焦点事件
editor.value.onDidBlurEditorText(() => {});
});
// 获取编辑框内容
function getCodeContext() {
codeContent.value = toRaw(editor.value).getValue();
return console.log(codeContent.value);
}
// 自动格式化代码
function format() {
toRaw(editor.value).trigger("anything", "editor.action.formatDocument");
// 或者
// this.editor.getAction(['editor.action.formatDocument']).run()
}
// 修改主题
function handleTheme() {
monaco.editor.setTheme("vs");
}
</script>
<style scoped>
#app {
height: 500px;
}
</style>
第三步
// vue.config.js 配置代码提示插件
const { defineConfig } = require("@vue/cli-service");
const MonacoWebpackPlugin = require("monaco-editor-webpack-plugin");
module.exports = defineConfig({
transpileDependencies: true,
configureWebpack: {
plugins: [new MonacoWebpackPlugin()],
},
});
这篇好文章是转载于:编程之路
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 编程之路
- 本文地址: /boutique/detail/tanhggccbi
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
怎样阻止微信小程序自动打开
PHP中文网 06-13