欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

前端插件庫之vue3使用vue-codemirror插件的步驟和實例

 更新時間:2022年07月29日 13:14:44   作者:一只愛吃蘿卜的小兔子  
CodeMirror是一款基于JavaScript、面向語言的前端代碼編輯器,下面這篇文章主要給大家介紹了關(guān)于前端插件庫之vue3使用vue-codemirror插件的步驟和實例,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下

vue-codemirror

基于 CodeMirror ,適用于 Vue 的 Web 代碼編輯器。

使用

1.命令行安裝

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

如果運行官網(wǎng)例子時, 報錯:

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

可以在終端中安裝對應(yīng)文件, 解決問題

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

2.在需要的組件中配置

<template>
  <codemirror
    v-model="code"
    placeholder="Code gose here..."
    :style="{ height: '400px' }"
    :autofocus="true"
    :indent-with-tab="true"
    :tabSize="2"
    :extensions="extensions"
    @ready="log('ready', $event)"
    @change="log('change', $event)"
    @focus="log('focus', $event)"
    @blur="log('blur', $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('Hello, world!')`);
    const extensions = [javascript(), oneDark];

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

配置說明:

個人代碼編輯區(qū)Demo

代碼編輯區(qū)

支持代碼編輯區(qū), 滿足白天/黑夜主題切換, 滿足c++/python語言切換

不足, 沒有滿足代碼提示

組件代碼 vue3

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

  <codemirror
    v-model="code"
    placeholder="Code gose here..."
    :style="style"
    :mode="mode"
    :spellcheck="spellcheck"
    :autofocus="autofocus"
    :indent-with-tab="indentWithTab"
    :tabSize="tabSize"
    :extensions="extensions"
    @ready="log('ready', $event)"
    @change="log('change', $event)"
    @focus="log('focus', $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() {
    // 數(shù)據(jù)
    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。創(chuàng)建({擴展})
    });

    // 方法
    // 失去焦點時,使用已編輯的代碼
    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>

總結(jié)

到此這篇關(guān)于前端插件庫之vue3使用vue-codemirror插件的文章就介紹到這了,更多相關(guān)vue3使用vue-codemirror插件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Vue項目中使用fontawesome圖標庫的方法

    Vue項目中使用fontawesome圖標庫的方法

    fontawesome的圖標有免費版和專業(yè)版,本文主要使用free版本,一般free版本的圖標夠用,free圖標又劃分為三個圖標庫,主要有實心圖標solid、常規(guī)圖標regular及品牌圖標brand,根據(jù)需求去下載對應(yīng)的圖標庫,無須全部下載,對vue?fontawesome圖標庫相關(guān)知識感興趣的朋友一起看看吧
    2023-12-12
  • el-table嵌套el-popover處理卡頓的解決

    el-table嵌套el-popover處理卡頓的解決

    本文主要介紹了el-table嵌套el-popover處理卡頓的解決,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧
    2022-05-05
  • 詳解vue-cli中的ESlint配置文件eslintrc.js

    詳解vue-cli中的ESlint配置文件eslintrc.js

    本篇文章主要介紹了vue-cli中的ESlint配置文件eslintrc.js詳解 ,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • Vue中路由守衛(wèi)的具體使用

    Vue中路由守衛(wèi)的具體使用

    導(dǎo)航守衛(wèi)就是路由跳轉(zhuǎn)前、中、后過程中的一些鉤子函數(shù),本文詳細的介紹了Vue中路由守衛(wèi)的具體使用,具有一定的參考價值,感興趣的可以了解一下
    2021-12-12
  • vue使用better-scroll實現(xiàn)下拉刷新、上拉加載

    vue使用better-scroll實現(xiàn)下拉刷新、上拉加載

    這篇文章主要為大家詳細介紹了vue使用better-scroll實現(xiàn)下拉刷新、上拉加載,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • Vue實現(xiàn)路由過渡動效的4種方法

    Vue實現(xiàn)路由過渡動效的4種方法

    Vue 路由過渡是對 Vue 程序一種快速簡便的增加個性化效果的的方法,這篇文章主要介紹了Vue實現(xiàn)路由過渡動效的4種方法,感興趣的可以了解一下
    2021-05-05
  • Vue動態(tài)樣式綁定實例詳解

    Vue動態(tài)樣式綁定實例詳解

    眾所周知vue是操作dom元素的,那么如果有元素要動態(tài)綁定樣式,這種需求,還是要通過改變數(shù)據(jù)來改變視圖的樣式,下面這篇文章主要給大家介紹了關(guān)于Vue動態(tài)樣式綁定的相關(guān)資料,需要的朋友可以參考下
    2023-04-04
  • 淺談vue項目,訪問路徑#號的問題

    淺談vue項目,訪問路徑#號的問題

    這篇文章主要介紹了淺談vue項目,訪問路徑#號的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • 淺談vue中關(guān)于checkbox數(shù)據(jù)綁定v-model指令的個人理解

    淺談vue中關(guān)于checkbox數(shù)據(jù)綁定v-model指令的個人理解

    這篇文章主要介紹了淺談vue中關(guān)于checkbox數(shù)據(jù)綁定v-model指令的個人理解,v-model用于表單的數(shù)據(jù)綁定很常見,下面就來詳細的介紹一下
    2018-11-11
  • vue+element實現(xiàn)下拉菜單并帶本地搜索功能示例詳解

    vue+element實現(xiàn)下拉菜單并帶本地搜索功能示例詳解

    這篇文章主要介紹了vue+element實現(xiàn)下拉菜單并帶本地搜索功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-09-09

最新評論