vue2.x中monaco-editor的使用說明
更新時間:2022年08月04日 09:23:50 作者:風(fēng)如也
這篇文章主要介紹了vue2.x中monaco-editor的使用說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
vue2中使用monaco-editor
安裝
注意兩個庫的版本指定
npm install monaco-editor@0.30.1 --save-dev npm install monaco-editor-webpack-plugin@6.0.0 --save-dev
配置
vue.config.js中配置
const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin')
module.exports = {
? configureWebpack: {
? ? plugins: [
? ? ? new MonacoWebpackPlugin()
? ? ]
? }
}創(chuàng)建MonacoEditor公共組件
<template> ? <div ref="editorContainer" class="editor"></div> </template>
<script>
import * as monaco from 'monaco-editor';
export default {
? name: 'MonacoEditor',
? data() {
? ? return {
? ? ? code: '',
? ? ? editor: null
? ? }
? },
? mounted() {
? ? this.init()
? },
? methods: {
? ? init() {
? ? ? // 初始化編輯器
? ? ? this.editor = monaco.editor.create(this.$refs.editorContainer, {
? ? ? ? value: this.code,
? ? ? ? language: 'javascript',
? ? ? ? tabSize: 2,
? ? ? ? scrollBeyondLastLine: false,
? ? ? ? automaticLayout: true, // 自動布局
? ? ? ? readOnly: false
? ? ? })
? ? ? console.log(this.editor)
? ? ? // 監(jiān)聽內(nèi)容變化
? ? ? this.editor.onDidChangeModelContent(() => {
? ? ? })
? ? ? // 監(jiān)聽失去焦點事件
? ? ? this.editor.onDidBlurEditorText((e) => {
? ? ? ? console.log(e)
? ? ? });
? ? },
? ? // 獲取編輯框內(nèi)容
? ? getCodeContext() {
? ? ? return this.editor.getValue()
? ? },
? ? // 自動格式化代碼
? ? format() {
? ? ? this.editor.trigger('anything', 'editor.action.formatDocument')
? ? ? // 或者
? ? ? // this.editor.getAction(['editor.action.formatDocument']).run()
? ? },
? ? changeEditor() {
? ? ? if (this.editor === null) {
? ? ? ? this.init()
? ? ? }
? ? ? const oldModel = this.editor.getModel()
? ? ? const newModel = monaco.editor.createModel(
? ? ? ? this.code,
? ? ? ? 'javascript'
? ? ? )
? ? ? if (oldModel) {
? ? ? ? oldModel.dispose()
? ? ? }
? ? ? this.editor.setModel(newModel)
? ? }
? }
}
</script><style scoped>
.editor {
? width: 100%;
? min-height: 400px;
}
</style>父組件中使用
<template> ? <div> ? ? <monaco-editor></monaco-editor> ? </div> </template>
<script>
import MonacoEditor from '@/components/MonacoEditor'
export default {
? name: 'Test6',
? components: {
? ? MonacoEditor
? }
}
</script>使用vue-monaco-editor遇到的坑
編輯器重復(fù)加載上次編輯器中的內(nèi)容,不會被新的內(nèi)容替代
直接上代碼
給MonacoEditor加上屬性key
?? ? ?<MonacoEditor ? ? ? ? width="100%" ? ? ? ? height="537" ? ? ? ? :key="randomkey" ? ? ? ? language="html" ? ? ? ? theme="vs-dark" ? ? ? ? :code="code" ? ? ? > ? ? ? </MonacoEditor>
每次重新給code賦值時,就重新獲取一次隨機數(shù)賦值給key
data() {
? ? return {
? ? ? randomkey: 123,
? ? }
? }
methods: {
?? ?// 每次重新給code賦值時,就重新調(diào)用一下下面這個方法
?? ?createRandomkey(){
? ? ? this.randomkey = Math.floor(Math.random()*(10,1000000012313))
? ? },
}編輯器editorOptions上的配置無法生效
<MonacoEditor
? ? ? ? width="100%"
? ? ? ? height="537"
? ? ? ? :key="randomkey"
? ? ? ? language="html"
? ? ? ? theme="vs-dark"
? ? ? ? :code="code"
? ? ? ? :editorOptions="options"
? ? ? ? @mounted="seeOnMounted"
>
// 在data中設(shè)置無法生效
options: {
? ? ?readOnly: true
},可以在@mounted方法中根據(jù)editor進(jìn)行設(shè)置
seeOnMounted(editor) {
? ? ? this.seeEditor = editor
? ? ? this.seeEditor.updateOptions({
? ? ? ? readOnly: true, //是否只讀
? ? ? })
? ? },以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue使用keep-alive如何實現(xiàn)多頁簽并支持強制刷新
這篇文章主要介紹了vue使用keep-alive如何實現(xiàn)多頁簽并支持強制刷新,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08
Vue監(jiān)控路由與路由參數(shù), 刷新當(dāng)前頁面數(shù)據(jù)的方法匯總
這篇文章主要介紹了Vue監(jiān)控路由與路由參數(shù), 刷新當(dāng)前頁面數(shù)據(jù)的幾種方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2023-10-10

