最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue使用引用庫(kù)中的方法附源碼

 更新時(shí)間:2021年07月24日 10:47:37   作者:后青春的晴詩(shī)  
當(dāng)vue使用庫(kù)中的getvalue方法時(shí),需要調(diào)用相關(guān)方法,通過(guò)定義ref=“”,使用this.$refs.exampleEditor._setValue(''),具體示例代碼參考下本文,對(duì)vue使用引用庫(kù)中的方法,感興趣的朋友一起看看吧

monaco-editor-vue的官方源碼如下

Index.js

import * as monaco from 'monaco-editor/esm/vs/editor/editor.api';

function noop() { }

export { monaco };

export default {
  name: 'MonacoEditor',
  props: {
    diffEditor: { type: Boolean, default: false },      //是否使用diff模式
    width: {type: [String, Number], default: '100%'},
    height: {type: [String, Number], default: '100%'},
    original: String,       //只有在diff模式下有效
    value: String,
    language: {type: String, default: 'javascript'},
    theme: {type: String, default: 'vs'},
    options: {type: Object, default() {return {};}},
    editorMounted: {type: Function, default: noop},
    editorBeforeMount: {type: Function, default: noop}
  },

  watch: {
    options: {
      deep: true,
      handler(options) {
        this.editor && this.editor.updateOptions(options);
      }
    },

    value() {
      this.editor && this.value !== this._getValue() && this._setValue(this.value);
    },

    language() {
      if(!this.editor) return;
      if(this.diffEditor){      //diff模式下更新language
        const { original, modified } = this.editor.getModel();
        monaco.editor.setModelLanguage(original, this.language);
        monaco.editor.setModelLanguage(modified, this.language);
      }else
        monaco.editor.setModelLanguage(this.editor.getModel(), this.language);
    },

    theme() {
      this.editor && monaco.editor.setTheme(this.theme);
    },

    style() {
      this.editor && this.$nextTick(() => {
        this.editor.layout();
      });
    }
  },

  computed: {
    style() {
      return {
        width: !/^\d+$/.test(this.width) ? this.width : `${this.width}px`,
        height: !/^\d+$/.test(this.height) ? this.height : `${this.height}px`
      }
    }
  },

  mounted () {
    this.initMonaco();
  },

  beforeDestroy() {
    this.editor && this.editor.dispose();
  },

  render (h) {
    return (
      <div class="monaco_editor_container" style={this.style}></div>
    );
  },

  methods: {
    initMonaco() {
      const { value, language, theme, options } = this;
      Object.assign(options, this._editorBeforeMount());      //編輯器初始化前
      this.editor = monaco.editor[this.diffEditor ? 'createDiffEditor' : 'create'](this.$el, {
        value: value,
        language: language,
        theme: theme,
        ...options
      });
      this.diffEditor && this._setModel(this.value, this.original);
      this._editorMounted(this.editor);      //編輯器初始化后
    },

    _getEditor() {
      if(!this.editor) return null;
      return this.diffEditor ? this.editor.modifiedEditor : this.editor;
    },

    _setModel(value, original) {     //diff模式下設(shè)置model
      const { language } = this;
      const originalModel = monaco.editor.createModel(original, language);
      const modifiedModel = monaco.editor.createModel(value, language);
      this.editor.setModel({
        original: originalModel,
        modified: modifiedModel
      });
    },

    _setValue(value) {
      let editor = this._getEditor();
      if(editor) return editor.setValue(value);
    },

    _getValue() {
      let editor = this._getEditor();
      if(!editor) return '';
      return editor.getValue();
    },

    _editorBeforeMount() {
      const options = this.editorBeforeMount(monaco);
      return options || {};
    },

    _editorMounted(editor) {
      this.editorMounted(editor, monaco);
      if(this.diffEditor){
        editor.onDidUpdateDiff((event) => {
          const value = this._getValue();
          this._emitChange(value, event);
        });
      }else{
        editor.onDidChangeModelContent(event => {
          const value = this._getValue();
          this._emitChange(value, event);
        });
      }
    },

    _emitChange(value, event) {
      this.$emit('change', value, event);
      this.$emit('input', value);
    }
  }
}

使用了vue想使用如上庫(kù)中的_getValue方法怎么調(diào)呢?

定義ref=“”,使用this.$refs.exampleEditor._setValue('')

參考如下代碼

test.vue

<template>
  <div>
    <MonacoEditor ref="exampleEditor" width="100%" height="300" theme="vs-dark" language="javascript" :options="options" @change="codeInput" />
  </div>
</template>
<script>
import MonacoEditor from 'monaco-editor-vue'
export default {
  components: {
    MonacoEditor
  },
  data() {
    return {
    }
  },
  beforeCreate() {
  },

  mounted() {
  },
  methods: {
    this.$refs.exampleEditor._setValue('')
  }
}

到此這篇關(guān)于vue使用引用庫(kù)中的方法附源碼的文章就介紹到這了,更多相關(guān)vue使用引用庫(kù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue-cli3.0項(xiàng)目打包后如何修改訪問(wèn)后端地址

    vue-cli3.0項(xiàng)目打包后如何修改訪問(wèn)后端地址

    這篇文章主要介紹了vue-cli3.0項(xiàng)目打包后如何修改訪問(wèn)后端地址,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • vue實(shí)現(xiàn)列表無(wú)縫滾動(dòng)

    vue實(shí)現(xiàn)列表無(wú)縫滾動(dòng)

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)列表無(wú)縫滾動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • 解決vue項(xiàng)目 build之后資源文件找不到的問(wèn)題

    解決vue項(xiàng)目 build之后資源文件找不到的問(wèn)題

    這篇文章主要介紹了解決vue項(xiàng)目 build之后資源文件找不到的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-09-09
  • 關(guān)于配置babel-plugin-import報(bào)錯(cuò)的坑及解決

    關(guān)于配置babel-plugin-import報(bào)錯(cuò)的坑及解決

    這篇文章主要介紹了關(guān)于配置babel-plugin-import報(bào)錯(cuò)的坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • Element控件Tree實(shí)現(xiàn)數(shù)據(jù)樹(shù)形結(jié)構(gòu)的示例代碼

    Element控件Tree實(shí)現(xiàn)數(shù)據(jù)樹(shù)形結(jié)構(gòu)的示例代碼

    我們?cè)陂_(kāi)發(fā)中肯定會(huì)遇到用樹(shù)形展示數(shù)據(jù)的需求,本文主要介紹了Element控件Tree實(shí)現(xiàn)數(shù)據(jù)樹(shù)形結(jié)構(gòu)的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-08-08
  • Vue-CLI項(xiàng)目中路由傳參的方式詳解

    Vue-CLI項(xiàng)目中路由傳參的方式詳解

    這篇文章主要介紹了Vue-CLI項(xiàng)目中路由傳參的方式詳解,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-09-09
  • vue路由組件路徑如何用變量形式動(dòng)態(tài)引入

    vue路由組件路徑如何用變量形式動(dòng)態(tài)引入

    這篇文章主要介紹了vue路由組件路徑如何用變量形式動(dòng)態(tài)引入問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • Vue.js組件間通信方式總結(jié)【推薦】

    Vue.js組件間通信方式總結(jié)【推薦】

    組件之間通信分為三種:父-子;子-父;跨級(jí)組件通信。下面,就組件間如何通信做一些總結(jié)。需要的朋友可以參考下
    2018-11-11
  • 使用Vue手寫(xiě)一個(gè)對(duì)話框

    使用Vue手寫(xiě)一個(gè)對(duì)話框

    相信大家之前都寫(xiě)過(guò)一些組件,尤其是這樣的彈窗組件,這篇文章主要來(lái)和大家聊聊如何使用Vue手寫(xiě)一個(gè)對(duì)話框,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-04-04
  • elementui?el-table底層背景色修改簡(jiǎn)單方法

    elementui?el-table底層背景色修改簡(jiǎn)單方法

    最近在做項(xiàng)目的時(shí)候遇到個(gè)需求,需要修改el-table背景色,這里給大家總結(jié)下,這篇文章主要給大家介紹了關(guān)于elementui?el-table底層背景色修改的相關(guān)資料,需要的朋友可以參考下
    2023-10-10

最新評(píng)論

通渭县| 茂名市| 荆门市| 吉林市| 仙居县| 乌拉特前旗| 浠水县| 遂昌县| 朝阳市| 大厂| 曲阳县| 阿拉善左旗| 肇东市| 图片| 津南区| 本溪| 青冈县| 武乡县| 罗甸县| 安丘市| 芮城县| 淳化县| 孟州市| 枝江市| 治县。| 太原市| 凌云县| 资中县| 邯郸县| 普定县| 二连浩特市| 彭水| 务川| 巢湖市| 杂多县| 揭阳市| 永顺县| 华坪县| 海盐县| 潢川县| 忻州市|