1. npm i monaco-editor
2. 在代码引用
<div class="monaco-editor" id="monaco-editor"></div>
const value = ref(`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>圆</title>
</head>
<body>
<div id="vszContainer"></div>
<script src="/index.js" type="text/javascript"><\/script>
<script>
......
<\/script>
</body>
</html>
`);
const options = ref({
colorDecorators: true,
lineHeight: 24,
tabSize: 2,
});
const initMonaco = () => {
const editorDom = document.getElementById("monaco-editor");
editor.value = monaco.editor.create(editorDom, {
value: value.value,
language: "html",
theme: "vs-dark",
options: options.value,
});
};
3. 启动在vue中报如下的错
vue中在src/main.js下引入下面代码就可以解决。
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";
self.MonacoEnvironment = {
getWorker(_, label) {
if (label === "json") {
return new jsonWorker();
}
if (label === "css" || label === "scss" || label === "less") {
return new cssWorker();
}
if (label === "html" || label === "handlebars" || label === "razor") {
return new htmlWorker();
}
if (label === "typescript" || label === "javascript") {
return new tsWorker();
}
return new editorWorker();
},
};
4. 报错 script Invalid end tag. script标签如下引用就可以呢
<script src="/index.js" type="text/javascript"><\/script>