Vue如何使用引用库,调用引用库的方法怎样做
Admin 2022-06-15 群英技术资讯 969 次浏览
今天小编跟大家讲解下有关“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('')
}
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
随着 Vite2 的发布并日趋稳定,现在越来越多的项目开始尝试使用它。本文我们就介绍了Vite创建项目的实现步骤,感兴趣的可以了解一下
这篇文章主要介绍JS数组去重,关于去重实际应用中,最常用的方法就是使用Set,也可以使用第三方库lodash来处理,下面一起来看看文章是怎么介绍JS数组去重的
jQuery事件的具体使用是怎样,种类包括哪些?事件响应和触发器是jQuery编程中非常重要的技术,而我们程序员也需要掌握和运用jQuery的各种常见事件操作技巧,这在我们的编程有很重要的作用。
这篇文章主要为大家详细介绍了vue实现简单的购物车小案例,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
如果调用者在函数返回时无法获得预期的结果,但在未来需要通过一定的手段获得,那么函数是异步的。如果函数异步,调用后立即返回,但不会立即返回预期结果。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008