首頁 >web前端 >Vue.js >vue3如何使用vue-codemirror插件

vue3如何使用vue-codemirror插件

王林
王林轉載
2023-05-13 12:49:113278瀏覽

使用

1.命令列安裝

npm install vue-codemirror --save
// cnpm install vue-codemirror --save

如果執行官網範例時, 報錯:

@codemirror/lang- javascript
@codemirror/theme-one-dark

可以在終端機中安裝對應檔案, 解決問題

npm i  @codemirror/lang-javascript
npm i  @codemirror/theme-one-dark

2.在需要的元件中設定

<template>
  <codemirror
    v-model="code"
    placeholder="Code gose here..."
    :
    :autofocus="true"
    :indent-with-tab="true"
    :tabSize="2"
    :extensions="extensions"
    @ready="log(&#39;ready&#39;, $event)"
    @change="log(&#39;change&#39;, $event)"
    @focus="log(&#39;focus&#39;, $event)"
    @blur="log(&#39;blur&#39;, $event)"
  />
</template>

<script>
import { Codemirror } from "vue-codemirror";
import { javascript } from "@codemirror/lang-javascript";
import { oneDark } from "@codemirror/theme-one-dark";

import { ref } from "vue";
export default {
  components: {
    Codemirror,
  },
  setup() {
    const code = ref(`console.log(&#39;Hello, world!&#39;)`);
    const extensions = [javascript(), oneDark];

    return {
      code,
      extensions,
      log: console.log,
    };
  },
};
</script>

設定說明:

vue3如何使用vue-codemirror插件

個人程式碼編輯區Demo

#程式碼編輯區

#支援程式碼編輯區, 滿足白天/黑夜主題切換, 滿足c /python語言切換

不足, 沒有滿足程式碼提示

#元件代碼vue3

<template>
  <button @click="changeTheme($event)">黑夜</button>
  <button @click="changeMode($event)">C++</button>

  <codemirror
    v-model="code"
    placeholder="Code gose here..."
    :
    :mode="mode"
    :spellcheck="spellcheck"
    :autofocus="autofocus"
    :indent-with-tab="indentWithTab"
    :tabSize="tabSize"
    :extensions="extensions"
    @ready="log(&#39;ready&#39;, $event)"
    @change="log(&#39;change&#39;, $event)"
    @focus="log(&#39;focus&#39;, $event)"
    @blur="useEditedCode"
  />
</template>

<script>
import { Codemirror } from "vue-codemirror";
import { python } from "@codemirror/lang-python";
import { cpp } from "@codemirror/lang-cpp";

import { oneDark } from "@codemirror/theme-one-dark";
import "codemirror/addon/hint/show-hint.css";

import { reactive, ref, toRefs } from "vue";

export default {
  components: {
    Codemirror,
  },
  setup() {
    // 数据
    const code = ref(``);
    let selectValue = "cpp";
    let dateTime = "黑夜";
    const options = reactive({
      style: { height: "400px" },
      mode: "text/x-c++src",
      spellcheck: true,
      autofocus: true,
      indentWithTab: true,
      tabSize: 2,
      extensions: [cpp(), oneDark], //传递给CodeMirror EditorState。创建({扩展})
    });

    // 方法
    // 失去焦点时,使用已编辑的代码
    function useEditedCode() {
      console.log("@@@blur@@@code:", code.value);
      console.log("@@@blur@@@cpp:", cpp);
    }

    // 改变主题
    function changeTheme(e) {
      console.log("options.extensions:", options.extensions);
      if (e.target.innerHTML === "黑夜") {
        options.extensions = [];
        dateTime = e.target.innerHTML = "白天";
      } else {
        options.extensions = [oneDark];
        dateTime = e.target.innerHTML = "黑夜";
      }
    }
    // 改变模式
    function changeMode(e) {
      console.log("selectValue:", selectValue);
      if (selectValue === "cpp") {
        if (dateTime === "黑夜") options.extensions = [python(), oneDark];
        else options.extensions = [python()];
        selectValue = "python";
        e.target.innerHTML = "python";
        options.mode = "text/x-python";
      } else {
        if (dateTime === "黑夜") options.extensions = [cpp(), oneDark];
        else options.extensions = [cpp()];
        selectValue = "cpp";
        e.target.innerHTML = "C++";
        options.mode = "text/x-c++src";
      }
    }
    // 返回
    return {
      code,
      selectValue,
      dateTime,
      ...toRefs(options),
      log: console.log,
      useEditedCode,
      changeTheme,
      changeMode,
    };
  },
};
</script>

vue3如何使用vue-codemirror插件

vue3如何使用vue-codemirror插件

vue3如何使用vue-codemirror插件

以上是vue3如何使用vue-codemirror插件的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除