欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue2.x中monaco-editor的使用說(shuō)明

 更新時(shí)間:2022年08月04日 09:23:50   作者:風(fēng)如也  
這篇文章主要介紹了vue2.x中monaco-editor的使用說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

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)制刷新

    這篇文章主要介紹了vue使用keep-alive如何實(shí)現(xiàn)多頁(yè)簽并支持強(qiáng)制刷新,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • 詳解如何在vue項(xiàng)目中引入elementUI組件

    詳解如何在vue項(xiàng)目中引入elementUI組件

    這篇文章主要介紹了詳解如何在vue項(xiàng)目中引入elementUI組件,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-02-02
  • vue-router實(shí)現(xiàn)嵌套路由的講解

    vue-router實(shí)現(xiàn)嵌套路由的講解

    今天小編就為大家分享一篇關(guān)于vue-router實(shí)現(xiàn)嵌套路由的講解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-01-01
  • Vue監(jiān)控路由與路由參數(shù), 刷新當(dāng)前頁(yè)面數(shù)據(jù)的方法匯總

    Vue監(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

    簡(jiǎn)單實(shí)現(xiàn)一個(gè)vue公式編輯器組件demo

    這篇文章主要介紹了輕松實(shí)現(xiàn)一個(gè)簡(jiǎn)單的vue公式編輯器組件示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2024-01-01
  • 手把手教你使用vue-cli腳手架(圖文解析)

    手把手教你使用vue-cli腳手架(圖文解析)

    本篇文章主要介紹了手把手教你使用vue-cli腳手架(圖文解析),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-11-11
  • 詳解vue3中的非父子組件傳值

    詳解vue3中的非父子組件傳值

    這篇文章主要為大家介紹了vue3中的非父子組件傳值,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2021-12-12
  • 使用Vue實(shí)現(xiàn)網(wǎng)頁(yè)截圖和截屏功能

    使用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-10
  • axios取消請(qǐng)求CancelToken的用法示例代碼

    axios取消請(qǐng)求CancelToken的用法示例代碼

    Axios提供了取消請(qǐng)求的功能,可以通過(guò)使用CancelToken來(lái)實(shí)現(xiàn),這篇文章主要給大家介紹了關(guān)于axios取消請(qǐng)求CancelToken的用法,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-10-10
  • Vue中的生命周期詳細(xì)解讀

    Vue中的生命周期詳細(xì)解讀

    這篇文章主要介紹了Vue中的生命周期詳細(xì)解讀,每個(gè) Vue 組件實(shí)例在創(chuàng)建時(shí)都需要經(jīng)歷一系列的初始化步驟,比如設(shè)置好數(shù)據(jù)偵聽(tīng),編譯模板,掛載實(shí)例到 DOM,以及在數(shù)據(jù)改變時(shí)更新 DOM,需要的朋友可以參考下
    2023-08-08

最新評(píng)論