在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

在vue项目中使用codemirror插件实现代码编辑器功能(代码高亮显示及自动提示的更多相关文章

  1. 实战:vue项目中导入swiper插件

    版本选择 swiper是个常用的插件,现在已经迭代到了第四代:swiper4.常用的版本是swiper3和swiper4,我选择的是swiper3. 安装 安装swiper3的最新版本3.4.2: n ...

  2. vue项目中的相关插件

    所有安装都是cd到该项目目录中安装 -S代表将插件添加到项目中的package.json文件 1.iview 是一套基于 Vue.js 的开源 UI 组件库,主要服务于 PC 界面的中后台产品 cnp ...

  3. 热力图 vue 项目中使用热力图插件 “heatmap.js”(保姆式教程)

    我现在写的这项目是用CDN引入 heatmap.js, 可根据自己项目情况使用哪种方式引入插件. 官网地址 "https://www.patrick-wied.at/static/heatm ...

  4. Vue项目中使用jquery插件

    1.引入jquery,并且在vue.config.js里配置 config.plugin('provide') .use(webpack.ProvidePlugin, [{ $: 'jquery', ...

  5. vue项目中使用swiper插件遇到的坑

    <style scoped> .swiper-pagination-bullets  >>> .swiper-pagination-bullet-active {     ...

  6. 转:如何在Vue项目中使用vw实现移动端适配

    https://www.w3cplus.com/mobile/vw-layout-in-vue.html 有关于移动端的适配布局一直以来都是众说纷纭,对应的解决方案也是有很多种.在<使用Flex ...

  7. 在Vue项目中使用vw实现移动端适配

    有关于移动端的适配布局一直以来都是众说纷纭,对应的解决方案也是有很多种.在<使用Flexible实现手淘H5页面的终端适配>提出了Flexible的布局方案,随着viewport单位越来越 ...

  8. 如何在Vue项目中使用vw实现移动端适配(转)

    有关于移动端的适配布局一直以来都是众说纷纭,对应的解决方案也是有很多种.在<使用Flexible实现手淘H5页面的终端适配>提出了Flexible的布局方案,随着viewport单位越来越 ...

  9. Vue项目中使用vw实现移动端适配

    我们在vue移动端项目中的适配一般都采用rem,但是rem也不是能兼容所有的终端. 随着viewport单位越来越受到众多浏览器的支持,下面将简单介绍怎么实现vw的兼容问题,用vw代替rem 当我们采 ...

随机推荐

  1. Tomcat服务器的安装与配置

    安装 输入网址进入Tomcat的官网            在左边导航栏选择对应下载的版本            下载安装包形式             下载并解压到我们欲放入的目录中 配置      ...

  2. [UIImage _isCached]: message sent to deallocated instance

    本文转载至 http://zhuhaibobb.blog.163.com/blog/static/2744006720124191633375/       这几天做了个APP打开20份钟左右就强制退 ...

  3. SaltStack远程执行-返回MySQL

    上一篇:SaltStack远程执行-模块 参考官方文档:https://docs.saltstack.com/en/latest/ref/returners/all/salt.returners.my ...

  4. Exchange Pause or stop transport service

    The Microsoft Exchange Transport service is a service available both on the Microsoft Exchange Serve ...

  5. python console

    print(sys.stdout.encoding, locale.getpreferredencoding ()) windows console : chcp 65001; 在设置了这个环境变量时 ...

  6. 安装和使用PhantomJS

    一.安装PhantomJS(linux环境安装) 将PhantomJS下载在/usr/local/src/packet/目录下(这个看个人喜好) 操作系统:CentOS 7 64-bit 1.下载地址 ...

  7. 前端开发 - Bootstrap

    一.bootstrap简介 插件 == js 在js 的基础上写了一些功能 一个插件就是一个功能/方法组件 = js + css + html 组件包含着插件 官网: http://www.bootc ...

  8. Ubuntu 12.04安装Google Chrome(转)

    下载google chrome deb包,下载地址:https://www.google.com/chrome/browser/desktop/index.html,google的网站被墙了,如果你下 ...

  9. CentOS yum 安装node.js

    第一步: curl --silent --location https://rpm.nodesource.com/setup_10.x | sudo bash - 第二步: sudo yum -y i ...

  10. 前端 javascript 数据类型 布尔类型

    python 是大写 True javascript 是小写 true false 也是 布尔类型仅包含真假,与Python不同的是其首字母小写. ==      比较值相等 !=       不等于 ...