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

vue使用引用庫中的方法附源碼

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

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模式下設置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想使用如上庫中的_getValue方法怎么調呢?

定義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('')
  }
}

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

相關文章

  • 一文搞懂Vue中computed和watch的區(qū)別

    一文搞懂Vue中computed和watch的區(qū)別

    這篇文章主要和大家詳細介紹一下Vue中computed和watch的使用與區(qū)別,文中通過示例為大家進行了詳細講解,對Vue感興趣的同學,可以學習一下
    2022-11-11
  • vue實現(xiàn)右鍵彈出菜單

    vue實現(xiàn)右鍵彈出菜單

    這篇文章主要為大家詳細介紹了vue實現(xiàn)右鍵彈出菜單,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • Vue開發(fā)高德地圖應用的最佳實踐

    Vue開發(fā)高德地圖應用的最佳實踐

    要在Web頁面中加入地圖,我推薦你使用高德地圖,下面這篇文章主要給大家介紹了關于Vue開發(fā)高德地圖應用的最佳實踐,需要的朋友可以參考下
    2021-07-07
  • vue data中如何獲取使用store中的變量

    vue data中如何獲取使用store中的變量

    這篇文章主要介紹了vue data中如何獲取使用store中的變量,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • vue3中通過ref獲取元素節(jié)點的實現(xiàn)

    vue3中通過ref獲取元素節(jié)點的實現(xiàn)

    這篇文章主要介紹了vue3中通過ref獲取元素節(jié)點的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • Vue2.0用 watch 觀察 prop 變化(不觸發(fā))

    Vue2.0用 watch 觀察 prop 變化(不觸發(fā))

    本篇文章主要介紹了Vue2.0用 watch 觀察 prop 變化(不觸發(fā)),非常具有實用價值,需要的朋友可以參考下
    2017-09-09
  • Vue淺析講解動態(tài)組件與緩存組件及異步組件的使用

    Vue淺析講解動態(tài)組件與緩存組件及異步組件的使用

    這篇文章主要介紹了Vue開發(fā)中的動態(tài)組件與緩存組件及異步組件的使用教程,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-09-09
  • Vue實現(xiàn)簡單跑馬燈特效

    Vue實現(xiàn)簡單跑馬燈特效

    這篇文章主要為大家詳細介紹了Vue實現(xiàn)簡單跑馬燈特效,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • Vue實現(xiàn)簡單基礎的圖片裁剪功能

    Vue實現(xiàn)簡單基礎的圖片裁剪功能

    這篇文章主要為大家詳細介紹了如何利用Vue2實現(xiàn)簡單基礎的圖片裁剪功能,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起了解一下
    2022-09-09
  • Vue動態(tài)添加屬性到data的實現(xiàn)

    Vue動態(tài)添加屬性到data的實現(xiàn)

    在vue中請求接口中,一個請求方法可能對應后臺兩個請求接口,所以請求參數(shù)就會有所不同。需要我們先設置共同的參數(shù),然后根據(jù)條件動態(tài)添加參數(shù)屬性
    2022-08-08

最新評論