簡體   English   中英

從 Vue 2 中的 Monaco Editor 獲取值

[英]Get values from Monaco Editor in Vue 2

我是 Vuejs 的新手,必須將 Vue2 與 Monaco Editor 集成。 我想獲取用戶輸入的值。 我嘗試了幾種方法,但無法獲得價值。 提前致謝!

這是我的 Editor.vue 文件。

<template>
  <div id="editor" ref="editor"></div>
</template>

<script>
import * as monaco from "monaco-editor";

export default {
  name: "CodeEditor",
  mounted() {
    const editorOptions = {
      value: [
        "function greeting() {",
        '\tconsole.log("Test Monaco...);',
        "}",
      ].join("\n"),
      language: "text/javascript",
      minimap: { enabled: false },
      wordWrap: true,
      automaticLayout: true,
    };
    window.editor = monaco.editor.create(document.getElementById("editor"), editorOptions);
  },

  computed: {   
    getUserInput() {
      // how to get user input???
    },
  },
};
</script>

<style>
#editor {
  height: 500px;
  width: 100%;
  overflow: hidden;
}
</style>

使用 monaco 編輯器內置的getValue()方法很容易獲取值。

    <template>
      <div id="editor" ref="editor"></div>
    </template>
    
    <script>
    import * as monaco from "monaco-editor";
    
    export default {
      name: "CodeEditor",
      data() {
          editor: null,
          editorOptions: {
            value: [ "function greeting() {", '\tconsole.log("Test Monaco...);', "}", ].join("\n"),
          language: "text/javascript",
          minimap: { enabled: false },
          wordWrap: true,
          automaticLayout: true
        }
      },
      methods: {
         createEditor() {
            this.editor = monaco.editor.create(document.getElementById("editor"), this.editorOptions);
         },
         getEditorValue() {
            // Returns the current editor value
            return this.editor.getValue()
         },
      },
      mounted() {
        this.createEditor()
      },
    };
    </script>
    
    <style>
    #editor {
      height: 500px;
      width: 100%;
      overflow: hidden;
    }
    </style>

我在數據部分設置了你的變量,並添加了一個createEditorgetEditorValue方法,它們將幫助你更好地組織你的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM