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

vue3+vite+pnpm项目 使用monaco-editor常见问题

文章目录

    • 一、使用步骤
      • 1.1 安装插件
      • 1.2 配置 vite.config.js
      • 1.3 封装 json-editor 组件
      • 1.4 使用 json-editor 组件
    • 二、遇到报错
      • 2.1 `TypeError: Cannot read properties of undefined (reading 'toUrl')`
      • 2.2 `Cannot read properties of undefined (reading 'languageWorkers')`
    • 参考地址

一、使用步骤

Monaco Editor API

1.1 安装插件

pnpm install monaco-editor --save
pnpm install monaco-editor-webpack-plugin --save 
pnpm install --save-dev vite-plugin-monaco-editor

1.2 配置 vite.config.js

import monacoEditorPlugin from 'vite-plugin-monaco-editor';export default defineConfig({...plugins: [monacoEditorPlugin({}),],...
})

1.3 封装 json-editor 组件

/* 模块名称:json-editor */
<template><div ref="monacoDom" class="json-editor"></div>
</template>
<script lang="ts" setup>
import beautify from 'js-beautify'
import * as monaco from 'monaco-editor';const props = defineProps({modelValue: {type: String,default: ''},readOnly: {type: Boolean,default: false,},config: {type: Object,default() {return {};}}
});
const emits = defineEmits(['update:modelValue', 'change', 'ready'])
const monacoDom: Ref<HTMLElement | null> = ref(null);
let monacoInstance: monaco.editor.IStandaloneCodeEditor | null = null;watch(() => props.modelValue, (newValue) => {const value = monacoInstance?.getValue();if (newValue !== value) {monacoInstance?.setValue(props.modelValue)}
})
watch(() => props.readOnly, (readOnly) => {monacoInstance?.updateOptions({readOnly,});
})
onMounted(() => {monaco.languages.json.jsonDefaults.setDiagnosticsOptions({allowComments: true,validate: true,trailingCommas: 'ignore',schemaValidation: 'warning'})monaco.languages.json.jsonDefaults.setModeConfiguration({completionItems: false,tokens: true,colors: true,foldingRanges: true,diagnostics: true,})monacoInstance = monaco.editor.create(monacoDom.value as HTMLElement, {value: props.modelValue,language: 'json',automaticLayout: true,parameterHints: {enabled: true},minimap: {enabled: false,},wrappingStrategy: 'advanced',scrollBeyondLastLine: false,overviewRulerLanes: 0,scrollbar: {alwaysConsumeMouseWheel: false},hover: {enabled: true,above: false,},renderLineHighlight: 'none',fontSize: 14,readOnly: props.readOnly,...props.config})monacoInstance.onDidChangeModelContent(() => {emits('update:modelValue', monacoInstance?.getValue())emits('change', monacoInstance?.getValue())})emits('ready')
})
onActivated(() => {monacoInstance?.focus()
})
onBeforeUnmount(() => {monacoInstance?.dispose();
})
const format = () => {const formatStr = beautify(props.modelValue, { indent_size: 4 });monacoInstance?.setValue(formatStr)
}
const focus = () => {monacoInstance?.focus()
}
defineExpose({format,focus
});
</script>
<style lang="scss">
.json-editor {width: 100%;height: 100%;
}
</style>

1.4 使用 json-editor 组件

<template><json-editorref="jsonComponents":model-value="strJson"@update:model-value="handleChangeResponseJson"/>
</template>
<script lang="ts" setup>
import JsonEditor from '../common/json-editor.vue'const jsonComponents: Ref<null | { format: () => void }[]> = ref(null)
const strJson = ref('')//更改返回json数据
const handleChangeResponseJson = (value: string) => {// 返回内容值,根据业务增加后面的逻辑
}
</script>

二、遇到报错

2.1 TypeError: Cannot read properties of undefined (reading 'toUrl')

出现上面的报错的原因是只安装了 monaco-editor 插件,未对模块加载配置,需安装对应的解析器 monaco-editor-webpack-pluginvite-plugin-monaco-editor

2.2 Cannot read properties of undefined (reading 'languageWorkers')

出现上面的报错的原因是 在 vite.config.js 配置 monacoEditorPlugin() 时,需要传默认值,monacoEditorPlugin({})

参考地址

https://blog.csdn.net/qq_37772059/article/details/125404159

相关文章:

  • 使用python进行图像处理—图像滤波(5)
  • 论文解读:交大港大上海AI Lab开源论文 | 宇树机器人多姿态起立控制强化学习框架(二)
  • 自动化测试认识与展望
  • Python训练营---DAY49
  • 深度解析:etcd 在 Milvus 向量数据库中的关键作用
  • UNECE R79——解读自动驾驶相关标准法规
  • 将 VSCode 的快捷键设置为与 IntelliJ IDEA 类似
  • 【开发技术】.Net使用FFmpeg视频特定帧上绘制内容
  • idea 设置git提交快捷键
  • AI高考志愿助手应用架构设计并上线实施运行
  • 虚拟机网络不通的问题(这里以win10的问题为主,模式NAT)
  • vue3+vite项目中使用.env文件环境变量方法
  • vSphere环境ubuntu24.04虚拟机从BIOS切换为EFI模式启动
  • 深入解析C#表达式求值:优先级、结合性与括号的魔法
  • node.js的初步学习
  • 前端性能优化-虚拟滚轮(Virtual Scroll)
  • 无菌药厂通信架构升级:MODBUS TCP转CANopen技术的精准控制应用
  • VUE3 ref 和 useTemplateRef
  • JAVA 线程池 BlockingQueue详解
  • LMKD(Low Memory Killer Daemon)原理初识
  • 买奢侈品代工厂做的产品的网站名/今日国内新闻最新消息10条
  • 驻马店做网站哪家好/百度seo关键词排名优化软件
  • 营销型网站建设必备功能/公司网站如何seo
  • 临沂定制网站建设公司/seo系统培训哪家好
  • 杭州认证网站建设/上海最专业的seo公司
  • 网站建设开源项目github/百度指数大数据分享平台