diff --git a/src/vendors/monaco-editor.js b/src/vendors/monaco-editor.js index c1384bf..12d1b24 100644 --- a/src/vendors/monaco-editor.js +++ b/src/vendors/monaco-editor.js @@ -1,5 +1,5 @@ import VueMonacoEditor, { loader } from '@guolao/vue-monaco-editor' -import { ref, watch, toRaw, h, withDirectives, resolveDirective } from 'vue' +import { ref, watch, toRaw, h, withDirectives, resolveDirective, defineAsyncComponent } from 'vue' const MonacoLoader = () => import('monaco-editor') const WorkerImporters = { @@ -153,13 +153,18 @@ export const getLoadingDiv = (attrs = {}) => { export default { install (app) { - app.component(VueMonacoEditor.name, { - setup (props) { - if (loader.__getMonacoInstance() === null) { - MonacoLoader().then(monaco => loader.config({ monaco })) + app.component(VueMonacoEditor.name, defineAsyncComponent({ + loader: async () => { + // 等待本地 Monaco 配置完成后再创建编辑器,避免触发默认 CDN 加载。 + const monaco = await MonacoLoader() + loader.config({ monaco }) + return { + setup (props) { + return () => h(VueMonacoEditor, props, () => [getLoadingDiv()]) + } } - return () => h(VueMonacoEditor, props, () => [getLoadingDiv()]) - } - }) + }, + loadingComponent: { render: () => getLoadingDiv() } + })) } }