在vue項(xiàng)目中使用codemirror插件實(shí)現(xiàn)代碼編輯器功能
在vue項(xiàng)目中使用codemirror插件實(shí)現(xiàn)代碼編輯器功能(代碼高亮顯示及自動(dòng)提示),具體內(nèi)容如下所示:
1、使用npm安裝依賴(lài)
npm install --save codemirror;
2、在頁(yè)面中放入如下代碼
<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鍵進(jìn)行代碼提示'
}
},
mounted () {
debugger
let mime = 'text/x-mariadb'
//let theme = 'ambiance'//設(shè)置主題,不設(shè)置的會(huì)使用默認(rèn)主題
let editor = CodeMirror.fromTextArea(this.$refs.mycode, {
mode: mime,//選擇對(duì)應(yīng)代碼編輯器的語(yǔ)言,我這邊選的是數(shù)據(jù)庫(kù),根據(jù)個(gè)人情況自行設(shè)置即可
indentWithTabs: true,
smartIndent: true,
lineNumbers: true,
matchBrackets: true,
//theme: theme,
// autofocus: true,
extraKeys: {'Ctrl': 'autocomplete'},//自定義快捷鍵
hintOptions: {//自定義提示選項(xiàng)
tables: {
users: ['name', 'score', 'birthDate'],
countries: ['name', 'population', 'size']
}
}
})
//代碼自動(dòng)提示功能,記住使用cursorActivity事件不要使用change事件,這是一個(gè)坑,那樣頁(yè)面直接會(huì)卡死
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、話不多說(shuō),直接上圖

總結(jié)
以上所述是小編給大家介紹的在vue項(xiàng)目中使用codemirror插件實(shí)現(xiàn)代碼編輯器功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
vue2.0+SVG實(shí)現(xiàn)音樂(lè)播放圓形進(jìn)度條組件
這篇文章主要為大家詳細(xì)介紹了Vue2.0+SVG實(shí)現(xiàn)音樂(lè)播放圓形進(jìn)度條組件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09
vite?+?electron-builder?打包配置詳解
這篇文章主要為大家介紹了electron基于vite?+?electron-builder?打包配置詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
element?ui中el-form-item的屬性rules的用法示例小結(jié)
這篇文章主要介紹了element?ui中el-form-item的屬性rules的用法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2024-07-07
vue學(xué)習(xí)記錄之動(dòng)態(tài)組件淺析
動(dòng)態(tài)組件指的是動(dòng)態(tài)切換組件的顯示與隱藏,這篇文章主要給大家介紹了關(guān)于vue學(xué)習(xí)記錄之動(dòng)態(tài)組件的相關(guān)資料,本文通過(guò)示例代碼介紹介紹的非常詳細(xì),需要的朋友可以參考下2022-10-10

