简体   繁体   English

在 Vue.js 组件中创建 Monaco Editor 的实例

[英]Create a instance of Monaco Editor in Vue.js component

I'm working on Monaco Editor with Vue.js and I cannot understand how Monaco has instantiated into the vue component: 1) I put in data() an editorEx object to use for the purpose, like this我正在使用 Vue.js 开发 Monaco 编辑器,但我无法理解 Monaco 是如何实例化到 vue 组件中的:1)我输入 data() 一个 editorEx object 用于此目的,就像这样

data() {
 editorEx: {}
}

2) I wrote an initMonaco function (called when vue component have mounted) that invokes the constructor 'monaco.editor.create(...)' like this 2)我写了一个 initMonaco function (当 vue 组件已安装时调用)调用构造函数 'monaco.editor.create(...)' 像这样

initMonaco() {
    this.editor = monaco.editor.create(...)
}

Quension: Where is that 'this.editor' variable? Quension:“this.editor”变量在哪里?

I've never wrote that variable (in component, in vuex) but is working perfectly.我从来没有写过那个变量(在组件中,在 vuex 中),但工作得很好。

Javascript does not look for a property that defined before, to set a value. Javascript 不查找之前定义的属性来设置值。 It finds the object and sets the value.它找到 object 并设置值。 If there's no such property, it creates.如果没有这样的属性,它就会创建。

On the other hand, it also matters how you define the function for what "this" means.另一方面,如何定义 function 的“this”含义也很重要。 I'll give an example on a Vue component data object:我将举一个关于 Vue 组件数据 object 的示例:

data() {
    return {
        first_object: {
            action() {
                // "this" refers to first_object
            }
        },
        second_object: {
            action: () => {
                // "this" refers to Vue instance
            }
        },
    };
}

I hope this explanation helps.我希望这个解释有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM