vue使用引用库中的方法附源码

当vue使用库中的getvalue方法时,需要调用相关方法,通过定义ref=“”,使用this.$refs.exampleEditor._setValue(\’\’),具体示例代码参考下本文,对vue使用引用库中的方法,感兴趣的朋友一起看看吧

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 (      <p class=\"monaco_editor_container\" style={this.style}></p>    );  },  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>  <p>    <MonacoEditor ref=\"exampleEditor\" width=\"100%\" height=\"300\" theme=\"vs-dark\" language=\"javascript\" :options=\"options\" @change=\"codeInput\" />  </p></template><script>import MonacoEditor from \'monaco-editor-vue\'export default {  components: {    MonacoEditor  },  data() {    return {    }  },  beforeCreate() {  },  mounted() {  },  methods: {    this.$refs.exampleEditor._setValue(\'\')  }}

到此这篇关于vue使用引用库中的方法附源码的文章就介绍到这了,更多相关vue使用引用库内容请搜索趣讯吧以前的文章或继续浏览下面的相关文章希望大家以后多多支持趣讯吧!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,请发送邮件至 55@qq.com 举报,一经查实,本站将立刻删除。转转请注明出处:https://www.szhjjp.com/n/3128.html

(0)
nan
上一篇 2021-07-24
下一篇 2021-07-24

相关推荐

  • 求婚需要注意的细节(男孩求婚注意事项)久久派

    女孩子都是喜欢浪漫的,也是非常喜欢仪式感的,所以男生如果想要给女生求婚的话是需要注意很多细节的,如果细节不到位的话,可能会导致求婚出现一些偏差,那么求婚需要注意些什么呢?下面就跟随久久派小编一起来了解一下求婚需要注意的细节,男孩求婚注意事项,男孩子向女孩子求婚准备什么。求婚需要注意的细节1、求婚场地男生在求婚前一定要提前选好求婚场地,求婚场地的具体选择可

    2021-08-10
    0
  • 大家认为合肥的二手别墅有升值空间么(合肥高新区别墅楼盘)

    [导读]这个问题前段时间正好遇到。一个老板在合肥有一套别墅,在县城有二套别墅九间门面。他只想留一套别墅住着,想抛售合肥与县城中的那两套别墅。我问为什么?人们现在都摇号购房产,等升值呢,你为什么你要卖呢?老板就是老板…这个问题前段时间正好遇到。一

    2022-01-13 热点头条
    0
  • 手机电池不耐用怎么恢复(手机耗电太快改善的小窍门)

    摘要:我们打开手机的【设置】,找到【电池】,下滑我们可以看到耗电排行,其中分为硬件和软件的耗电排行,我们可以找到不需要使用的软件,点击进去,然后点击【结束运行】。

    2023-04-13
    0
  • 越南队主帅:我没说过中国队球员只有(30,分钟体力)

    新华社阿联酋沙迦10月6日电近日有中国媒体报道,越南队主教练朴恒绪认为中国队球员只有30分钟体力。在6日举行的中国队与越南队的卡塔尔世界杯预选赛12强赛赛前新闻发布会上,朴恒绪对

    2021-10-07
    0
  • 李倩夺得拳击女子75公斤级银牌

    8月8日,在东京奥运会拳击女子75公斤级决赛中,李倩夺得银牌。

    2021-08-08 热点头条
    0
  • 如何速记(如何学汉字速记)

    如何速记(如何学汉字速记)经常有速记学习者咨询这样一个问题:现在速记方案有很多种,如何选择一个适合自己的速记方案来学习速记?今天我就把以前给一位学员的回答复制在这里,供初学者参考。速记,按字面意思来解释,就是快速的记录,也就是我们常说的用简

    2021-11-28
    0

发表回复

登录后才能评论