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

vue3 vite使用 monaco-editor 报错

报错:Unexpected usage at EditorSimpleWorker.loadForeignModule

修改配置:

 "monaco-editor-webpack-plugin": "^4.2.0",删除不用

版本:

 "monaco-editor": "^0.28.1",

 修改如下:

 optimizeDeps: {include: [`monaco-editor/esm/vs/language/json/json.worker`,`monaco-editor/esm/vs/language/css/css.worker`,`monaco-editor/esm/vs/language/html/html.worker`,`monaco-editor/esm/vs/language/typescript/ts.worker`,`monaco-editor/esm/vs/editor/editor.worker`,],},transpileDependencies: true,configureWebpack: {plugins: [],},

 文件中的代码

<template><divref="editorContainer"class="editor-container":class="{ 'no-suggestion': !props.suggestion }":style="{ height: `${props.myHeight ? props.myHeight : height + 'px'}` }"></div>
</template><script  setup lang="ts">
import { ref, watch, onMounted, onUnmounted, toRaw } from "vue";
//引入monaco-editor
import * as monaco from "monaco-editor";
import editorWorker from "monaco-editor/esm/vs/editor/editor.worker?worker";
import jsonWorker from "monaco-editor/esm/vs/language/json/json.worker?worker";
import cssWorker from "monaco-editor/esm/vs/language/css/css.worker?worker";
import htmlWorker from "monaco-editor/esm/vs/language/html/html.worker?worker";
import tsWorker from "monaco-editor/esm/vs/language/typescript/ts.worker?worker";const props = defineProps(["value","myHeight","suggestion","lineNumbers","fontSize","background",
]);
const emit = defineEmits(["change"]);const height = ref<any>(0);
const editor = ref<any>(null);
const editorContainer = ref<any>(null);monaco.editor.defineTheme("define-vs-dark", {base: "vs-dark",inherit: true,rules: [{foreground: props.background ? props.background : "#2f3031",token: "markdown.header",fontStyle: "bold",},],colors: {"editor.background": props.background ? props.background : "#2f3031","editorGutter.background": props.background ? props.background : "#2f3031",},
});const cssArr = ["css", "scss", "less"];
const jsonArr = ["json"];
const htmlArr = ["html", "handlebars", "razor"];
const tsArr = ["typescript", "javascript"];
const se: any = self;onMounted(() => {
//高亮及提示se.MonacoEnvironment = {getWorker(_: any, label: any) {if (jsonArr.includes(label)) {return new jsonWorker();}if (cssArr.includes(label)) {return new cssWorker();}if (htmlArr.includes(label)) {return new htmlWorker();}if (tsArr.includes(label)) {return new tsWorker();}return new editorWorker();},};//创建editor.value = monaco.editor.create(editorContainer.value, {value: props.value,theme: "define-vs-dark",folding: false,// cursorStyle: "line", //光标样式language: "typescript",selectOnLineNumbers: true, //显示行号roundedSelection: false,readOnly: false, // 只读automaticLayout: false, //自动布局glyphMargin: true, //字形边缘useTabStops: false,fontSize: props.fontSize ? props.fontSize : 14, //字体大小quickSuggestionsDelay: 100, //代码提示延时contextmenu: true,scrollBeyondLastLine: false,acceptSuggestionOnEnter: props.suggestion ? "on" : "off", // 接受输入建议 "on" | "off" | "smart"acceptSuggestionOnCommitCharacter: props.suggestion, // 接受关于提交字符的建议lineNumbers: props.lineNumbers ? "on" : "off",minimap: {enabled: false, // 关闭代码缩略图},});// 监听内容变化toRaw(editor.value).onDidChangeModelContent((e: any) => {sendValue();setContainerHeight();});setContainerHeight();// 监听失去焦点事件toRaw(editor.value).onDidBlurEditorText(() => {});
});
const reciveValue = () => {if (!editor.value) return;const currentValue = toRaw(editor.value).getValue();if (currentValue === props.value) return;toRaw(editor.value).setValue(props.value);
};
const sendValue = () => {if (!editor.value) return;const content = toRaw(editor.value).getValue()? toRaw(editor.value).getValue(): props.value;emit("change", content);
};const setContainerHeight = () => {const lineCount = toRaw(editor.value).getModel().getLineCount();const lineHeight = toRaw(editor.value).getOption(monaco.editor.EditorOption.lineHeight);height.value = lineCount * lineHeight + monaco.editor.EditorOption.lineHeight;
};
const watchValue = watch(() => props.value,() => {reciveValue();}
);
onMounted(() => {sendValue();
});
onUnmounted(() => {editor.value?.dispose();watchValue();
});
</script><style scoped lang="less">
.editor-container {width: 100%;
}
.no-suggestion {.suggest-widget {display: none !important;}
}
.editor-scrollable .lines-content {width: 100% !important;height: 100% !important;
}
</style>

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

相关文章:

  • 微信小程序获取蓝牙权限
  • GE 8920-PS-DC安全模块
  • UG\NX二次开发 使用BlockUI设计对话框时,如何设置默认的开发语言?
  • W5500-EVB-PICO进行UDP组播数据回环测试(九)
  • 24 WEB漏洞-文件上传之WAF绕过及安全修复
  • Python科研绘图--Task03
  • ssm端游游戏账号销售管理系统源码和论文
  • ssm+vue农家乐信息平台源码和论文
  • 安装启动yolo5教程
  • 封装redis 分布式锁 RedisCallback
  • 代码随想录算法训练营第17期第32天 | 122. 买卖股票的最佳时机 II、455.分发饼干、376. 摆动序列、53. 最大子序和
  • iOS HealthKit 介绍
  • Windows平台Unity下播放RTSP或RTMP如何开启硬解码?
  • 模板方法模式在JDBCTemplate中的应用
  • 如何在Debian中同步系统时间?Debian 系统时间配置(NTP服务)
  • 模板方法模式(十六)
  • 机器学习笔记之优化算法(十九)牛顿法与正则化
  • Java面试之单例模式的六种实现方式
  • re正则入门
  • C++ Day5
  • el-element:自定义参数
  • “分布式”与“集群”初学者的技术总结
  • C++day5(静态成员、类的继承、多继承)
  • 2023MySQL+MyBatis知识点整理
  • 【随笔】如何使用阿里云的OSS保存基础的服务器环境
  • 汽车电子笔记之:AUTOSA架构下的多核OS操作系统
  • 解决华为云ping不通的问题
  • 数据结构入门 — 链表详解_双向链表
  • 时序预测 | MATLAB实现PSO-KELM粒子群算法优化核极限学习机时间序列预测(含KELM、ELM等对比)
  • SSL/TLS协议的概念、工作原理、作用以及注意事项