在vue項目中使用codemirror插件實現代碼編輯器功能(代碼高亮顯示及自動提示


 

 

 

在vue項目中使用codemirror插件實現代碼編輯器功能(代碼高亮顯示及自動提示)

1、使用npm安裝依賴

npm install --save codemirror;

2、在頁面中放入如下代碼

<template>
  <textarea ref="mycode" class="codesql" v-model="code" style="height:200px;width:600px;"></textarea>
</template>

<script>
  import "codemirror/theme/ambiance.css";
  import "codemirror/lib/codemirror.css";
  import "codemirror/addon/hint/show-hint.css";

  let CodeMirror = require("codemirror/lib/codemirror");
  require("codemirror/addon/edit/matchbrackets");
  require("codemirror/addon/selection/active-line");
  require("codemirror/mode/sql/sql");
  require("codemirror/addon/hint/show-hint");
  require("codemirror/addon/hint/sql-hint");
    export default {
        name: "codeMirror",
      data () {
        return {
          code: '//按Ctrl鍵進行代碼提示'
        }
      },
      mounted () {
        debugger
        let mime = 'text/x-mariadb'
        //let theme = 'ambiance'//設置主題,不設置的會使用默認主題
        let editor = CodeMirror.fromTextArea(this.$refs.mycode, {
          mode: mime,//選擇對應代碼編輯器的語言,我這邊選的是數據庫,根據個人情況自行設置即可
          indentWithTabs: true,
          smartIndent: true,
          lineNumbers: true,
          matchBrackets: true,
          //theme: theme,
          // autofocus: true,
          extraKeys: {'Ctrl': 'autocomplete'},//自定義快捷鍵
          hintOptions: {//自定義提示選項
            tables: {
              users: ['name', 'score', 'birthDate'],
              countries: ['name', 'population', 'size']
            }
          }
        })
        //代碼自動提示功能,記住使用cursorActivity事件不要使用change事件,這是一個坑,那樣頁面直接會卡死
        editor.on('cursorActivity', function () {
          editor.showHint()
        })
      }
    }
</script>

<style>
  .codesql {
    font-size: 11pt;
    font-family: Consolas, Menlo, Monaco, Lucida Console, Liberation Mono, DejaVu Sans Mono, Bitstream Vera Sans Mono, Courier New, monospace, serif;
  }
</style>

 

3、話不多說,直接上圖

在這里插入圖片描述

4、這樣就把代碼編輯器實現啦,是不是so easy啊,你們自己去嘗試下吧

 

https://blog.csdn.net/mySpringandhibernate/article/details/84105095

 

另一篇文章: 使用Vue-codemirror使用總結

https://blog.csdn.net/weixin_43080277/article/details/83860629


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM