vue2.x中monaco-editor的使用說(shuō)明
vue2中使用monaco-editor
安裝
注意兩個(gè)庫(kù)的版本指定
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, // 自動(dòng)布局 ? ? ? ? readOnly: false ? ? ? }) ? ? ? console.log(this.editor) ? ? ? // 監(jiān)聽(tīng)內(nèi)容變化 ? ? ? this.editor.onDidChangeModelContent(() => { ? ? ? }) ? ? ? // 監(jiān)聽(tīng)失去焦點(diǎn)事件 ? ? ? this.editor.onDidBlurEditorText((e) => { ? ? ? ? console.log(e) ? ? ? }); ? ? }, ? ? // 獲取編輯框內(nèi)容 ? ? getCodeContext() { ? ? ? return this.editor.getValue() ? ? }, ? ? // 自動(dòng)格式化代碼 ? ? 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)容,不會(huì)被新的內(nèi)容替代
直接上代碼
給MonacoEditor加上屬性key
?? ? ?<MonacoEditor ? ? ? ? width="100%" ? ? ? ? height="537" ? ? ? ? :key="randomkey" ? ? ? ? language="html" ? ? ? ? theme="vs-dark" ? ? ? ? :code="code" ? ? ? > ? ? ? </MonacoEditor>
每次重新給code賦值時(shí),就重新獲取一次隨機(jī)數(shù)賦值給key
data() { ? ? return { ? ? ? randomkey: 123, ? ? } ? } methods: { ?? ?// 每次重新給code賦值時(shí),就重新調(diào)用一下下面這個(gè)方法 ?? ?createRandomkey(){ ? ? ? this.randomkey = Math.floor(Math.random()*(10,1000000012313)) ? ? }, }
編輯器editorOptions上的配置無(wú)法生效
<MonacoEditor ? ? ? ? width="100%" ? ? ? ? height="537" ? ? ? ? :key="randomkey" ? ? ? ? language="html" ? ? ? ? theme="vs-dark" ? ? ? ? :code="code" ? ? ? ? :editorOptions="options" ? ? ? ? @mounted="seeOnMounted" > // 在data中設(shè)置無(wú)法生效 options: { ? ? ?readOnly: true },
可以在@mounted方法中根據(jù)editor進(jìn)行設(shè)置
seeOnMounted(editor) { ? ? ? this.seeEditor = editor ? ? ? this.seeEditor.updateOptions({ ? ? ? ? readOnly: true, //是否只讀 ? ? ? }) ? ? },
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue使用keep-alive如何實(shí)現(xiàn)多頁(yè)簽并支持強(qiáng)制刷新
這篇文章主要介紹了vue使用keep-alive如何實(shí)現(xiàn)多頁(yè)簽并支持強(qiáng)制刷新,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08詳解如何在vue項(xiàng)目中引入elementUI組件
這篇文章主要介紹了詳解如何在vue項(xiàng)目中引入elementUI組件,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-02-02vue-router實(shí)現(xiàn)嵌套路由的講解
今天小編就為大家分享一篇關(guān)于vue-router實(shí)現(xiàn)嵌套路由的講解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-01-01Vue監(jiān)控路由與路由參數(shù), 刷新當(dāng)前頁(yè)面數(shù)據(jù)的方法匯總
這篇文章主要介紹了Vue監(jiān)控路由與路由參數(shù), 刷新當(dāng)前頁(yè)面數(shù)據(jù)的幾種方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-10-10簡(jiǎn)單實(shí)現(xiàn)一個(gè)vue公式編輯器組件demo
這篇文章主要介紹了輕松實(shí)現(xiàn)一個(gè)簡(jiǎn)單的vue公式編輯器組件示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01使用Vue實(shí)現(xiàn)網(wǎng)頁(yè)截圖和截屏功能
網(wǎng)頁(yè)截圖與截屏功能在許多Web應(yīng)用程序中都非常有用,Vue.js作為一個(gè)流行的JavaScript框架,提供了許多工具和庫(kù)來(lái)簡(jiǎn)化網(wǎng)頁(yè)截圖和截屏的實(shí)現(xiàn),本文將介紹如何使用Vue來(lái)實(shí)現(xiàn)一個(gè)網(wǎng)頁(yè)截圖和截屏功能的示例,包括使用html2canvas庫(kù)和vue-cropper庫(kù),需要的朋友可以參考下2023-10-10axios取消請(qǐng)求CancelToken的用法示例代碼
Axios提供了取消請(qǐng)求的功能,可以通過(guò)使用CancelToken來(lái)實(shí)現(xiàn),這篇文章主要給大家介紹了關(guān)于axios取消請(qǐng)求CancelToken的用法,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10