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)聽失去焦點(diǎ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賦值時,就重新獲取一次隨機(jī)數(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)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue.js實(shí)現(xiàn)watch屬性的示例詳解
本文討論了watch函數(shù)是如何利用副作用函數(shù)和options進(jìn)行封裝實(shí)現(xiàn)的,也通過調(diào)度函數(shù)去控制回調(diào)函數(shù)的立即執(zhí)行和執(zhí)行時機(jī),還可以解決競態(tài)問題,感興趣的可以了解一下2022-04-04
一步一步實(shí)現(xiàn)Vue的響應(yīng)式(對象觀測)
這篇文章主要介紹了一步一步實(shí)現(xiàn)Vue的響應(yīng)式(對象觀測),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
vue+elemen實(shí)現(xiàn)el-tooltip在文本超出區(qū)域后浮現(xiàn)
el-tooltip組件本身就是懸浮提示功能,在對它進(jìn)行二次封裝時,實(shí)現(xiàn)超出的文本浮現(xiàn),本文就來介紹一下vue+elemen實(shí)現(xiàn)el-tooltip在文本超出區(qū)域后浮現(xiàn),感興趣的可以了解一下2023-12-12
Vue中請求本地JSON文件并返回?cái)?shù)據(jù)的方法實(shí)例
在前端開發(fā)過程當(dāng)中,當(dāng)后臺服務(wù)器開發(fā)數(shù)據(jù)還沒完善,沒法與咱們交接時,咱們習(xí)慣在本地寫上一個json文件做模擬數(shù)據(jù)測試代碼效果是否有問題,下面這篇文章主要給大家介紹了關(guān)于Vue中請求本地JSON文件并返回?cái)?shù)據(jù)的相關(guān)資料,需要的朋友可以參考下2022-08-08
vue實(shí)現(xiàn)圖片預(yù)覽組件封裝與使用
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)圖片預(yù)覽組件封裝與使用,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-07-07
詳解vue 動態(tài)加載并注冊組件且通過 render動態(tài)創(chuàng)建該組件
這篇文章主要介紹了vue 動態(tài)加載并注冊組件且通過 render動態(tài)創(chuàng)建該組件,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-05-05

