安装
- npm install --save sql-formatter
- npm install --save vue-codemirror
<template>
<div>
<textarea ref="mycode" class="codesql" v-model="value"></textarea>
</div>
</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 {
data() {
return {
editor: null
}
},
props: {
value: {
type: String,
default: ''
},
sqlStyle: {
type: String,
default: 'default'
},
readOnly: {
type: [Boolean, String]
}
},
watch: {
newVal (newV, oldV) {
if (this.editor) {
this.$emit('changeTextarea', this.editor.getValue())
}
}
},
computed: {
newVal () {
if (this.editor) {
return this.editor.getValue()
}
}
},
mounted(){
let mime = 'text/x-sql'
this.editor = CodeMirror.fromTextArea(this.$refs.mycode, {
value: this.value,
mode: mime,
indentWithTabs: true,
smartIndent: true,
lineNumbers: true,
matchBrackets: true,
cursorHeight: 1,
lineWrapping: true,
readOnly: this.readOnly,
extraKeys: {'Ctrl': 'autocomplete'},
hintOptions: {
completeSingle: false,
}
})
this.editor.on('inputRead', () => {
this.editor.showHint()
})
},
methods: {
setVal () {
this.editor.setValue('')
}
}
}
</script>
<style lang="scss" scoped>
::v-deep .CodeMirror {
color: black;
direction: ltr;
line-height: 22px;
height: 260px;
.CodeMirror-scroll {
height:255px;
padding-bottom:0
}
}
.CodeMirror-hints{
z-index: 9999 !important;
}
</style>