前言:
最近在完成項目時,有使用編譯器進行在線編輯的功能,就選用了monaco-editor編譯器,但是實現功能之后,發現即使在編譯器展示的內容沒有超過編譯器高度的情況下,編譯器依舊存在滾動條,會展示大量的空白,為了解決這個問題,筆者采用了多種方式進行嘗試,最后終于解決,在此分享與各位小伙伴。
原本效果展示:

解決方案:添加scrollBeyondLastLine這個配置項
具體代碼展示:
<template><div class="container"><div class="code" ref="editorRef" /></div>
</template><script lang="ts" setup>
import { onMounted, ref } from "vue"
import * as Monaco from "monaco-editor"
const editorRef = ref(null)
const props = defineProps({code: {type: String,require: true},readOnly: {type: Boolean,require: true}
})onMounted(() => {const editor = Monaco.editor.create(editorRef.value,{value: props.code,language: "json",theme: "vs-dark",readOnly: props.readOnly,scrollBeyondLastLine: false //設置編輯器是否可以滾動到最后一行之后(添加這一行)}})}
})
</script>