当前位置: 首页 > 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

http://www.xdnf.cn/news/944929.html

相关文章:

  • 数据结构篇--分离链表vs线性探测
  • Redis的发布订阅模式与专业的 MQ(如 Kafka, RabbitMQ)相比,优缺点是什么?适用于哪些场景?
  • laravel8+vue3.0+element-plus搭建方法
  • Hugging Face、魔塔社区(MOTA)与OpenRouter:AI模型平台深度对比与实战指南
  • (七) 深度学习进阶:现代卷积神经网络技术解析与应用实践
  • <STC32G12K128入门第十九步>QT串口ISP更新上位机
  • Spring 框架(1)
  • 题山采玉:Day3
  • 3D Web轻量化引擎HOOPS Communicator赋能一线场景,支持本地化与动态展示?
  • 如何手撸一个最小化操作系统:从 0 到 1 掌握汇编-文件管理-内存页表-文件系统-上下文切换算法 MIT 经典教程 结合豆包ai
  • 如何控制electron的应用在指定的分屏上打开[特殊字符]
  • 计算机技术、互联网与 IT 前沿:量子计算、Web3.0 等趋势洞察及行业应用
  • 第21节 Node.js 多进程
  • WEB3全栈开发——面试专业技能点P2智能合约开发(Solidity)
  • GraphQL 实战篇:Apollo Client 配置与缓存
  • 技能伤害继承英雄属性【War3地图编辑器】进阶
  • 数据结构 - 栈与队列
  • 【Proteus仿真】【32单片机-A010】步进电机控制系统设计
  • “冒个泡泡”,排个序呗~:C语言版冒泡排序全解
  • 第22节 Node.js JXcore 打包
  • rknn优化教程(二)
  • Springboot项目集成Ai模型(阿里云百炼-DeepSeek)
  • 智能SQL优化工具PawSQL月度更新 | 推出免费私域部署社区版
  • 负载均衡LB》》LVS
  • 【仿生机器人】建模—— 图生3D 的几个办法
  • STM32 _main 里做了什么
  • Leetcode4(寻找两个正序数组的中位数)
  • 前端小程序面试题
  • Notepad++ 官方下载
  • uniapp 小程序 学习(一)