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 () {debuggerlet 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>

vue2 ts 类装饰版本组件

<template><textarea ref="container" v-model="value"></textarea>
</template><script lang="ts">
import Vue from 'vue';
import { Component, Ref, Model, Emit, Watch, Prop } from 'vue-property-decorator';
import CodeMirror from 'codemirror';
import 'codemirror/lib/codemirror.css';
import 'codemirror/mode/sql/sql';
import 'codemirror/theme/neat.css';
import 'codemirror/addon/scroll/simplescrollbars.css';
import 'codemirror/addon/scroll/simplescrollbars';
import 'codemirror/addon/hint/show-hint';
import 'codemirror/addon/hint/show-hint.css';
import 'codemirror/addon/hint/sql-hint';
import 'codemirror/mode/meta';
import 'codemirror/addon/selection/active-line';@Component
export default class CodeEditor extends Vue {@Ref('container') container!: HTMLTextAreaElement;@Model('change') value!: string;@Prop(Array) list?: string[];@Prop(Number) ch?: number;@Prop(String) height?: string;mirror: CodeMirror.Editor = CodeMirror(this.container);constructor() {super();this.changeListener = this.changeListener.bind(this);}mounted() {this.mirror = CodeMirror.fromTextArea(this.container, {value: this.value,mode: 'text/x-sql',theme: 'neat',styleActiveLine: true,readOnly: false,smartIndent: true,scrollbarStyle: 'overlay',lineNumbers: true,// matchBrackets: true,autofocus: true,// autoMatchParens: true,// styleSelectedText: true,indentUnit: 4,extraKeys: { Ctrl: 'autocomplete' },showHint: true,autocorrect: true,autocapitalize: true,});this.mirror.setSize('heigth', this.height);this.mirror.on('change', this.changeListener);}private changeListener(editor: CodeMirror.Editor, change: CodeMirror.EditorChangeLinkedList) {this.handleContentChange(this.mirror.getValue());if (change.origin === '+input' || change.origin === '*compose') {setTimeout(() => {const cursor = editor.getCursor();const str = editor.getLine(cursor.line);if (this.list && this.list.length) {// eslint-disable-next-lineconst hint = () => {return {from: { ch: this.ch || str.lastIndexOf(' ') + 1, line: cursor.line },to: { ch: str.length, line: cursor.line },list: [...(this.list || ['allen'])],};};editor.showHint({completeSingle: false,hint,});} else {this.mirror.showHint({ completeSingle: false, hint: CodeMirror.hint.sql });}}, 20);}}@Watch('value')handleValueChange(newValue: string) {if (newValue !== this.mirror.getValue()) {if (!newValue) {this.mirror.setValue('');return;}const value = newValue.split(' ').filter((item) => !!item).join(' ');this.mirror.setValue(value);this.mirror.focus();this.mirror.setCursor(newValue.length);}}@Emit('change')handleContentChange(content: string) {return content;}beforeDestroy() {this.mirror.off('change', this.changeListener);}
}
</script><style lang="less">
.CodeMirror {line-height: 1.5;
}
</style>

3、话不多说,直接上图

vue2项目使用codemirror插件实现代码编辑器功能相关推荐

  1. CodeMIrror.js在线代码编辑器简单使用

    代码高亮是程序员的刚需,不管是在笔记类,论坛类,博客类web网站中,都对代码高亮提出要求,不高亮的代码阅读体验很差,codeMirror是一个前端代码高亮库,使用方便.CodeMirror 是一款在线 ...

  2. 使用 CodeMirror 打造在线代码编辑器

    前提 写这个的目的是因为教学的过程中,需要减少环境搭建所浪费的时间,减少我的边际成本. CodeMirror 我觉得作为一款在线代码编辑器还是不错,也看到过有些网站用到过在线代码编辑,当然我不知道他们 ...

  3. vue2项目中按钮实现防抖、节流功能,包含封装代码以及使用

    1. 描述: 本文章是在vue2项目中对防抖 节流函数进行封装,在项目按钮中使用的实际应用. ,哪里有写的不太清楚或者有错的请各位大佬指正.留言:如果对您有所帮助,希望点赞给予支持和鼓励. 2. .v ...

  4. 【C++】Visual Studio教程(十二) -代码编辑器功能

    00. 目录 文章目录 00. 目录 01. 概述 02. 编辑器功能 03. 高级编辑功能 04. 导航和查找代码 05. 在基本代码中查找引用 06. 自定义编辑器 07. 附录 01. 概述 V ...

  5. 《Cocos Creator 代码编辑器》插件使用说明

    cocos 代码编辑插件一览: 使用教程: 使用前:日常工作中游戏脚本通常绑定在节点上,需要修改脚本代码时需打开第三方代码编辑器进行编辑.  使用后:使用本插件后点击场景的节点可直接在creator上 ...

  6. 【Vue3+Vite+TS项目集成ESlint +Prettier实现代码规范检查和代码格式化】

    目录 前言 创建项目 安装初始化ESlint 安装ESlint: 初始化ESlint: 安装配置Prettier 安装prettier: 配置prettier: 配置VScode保存时自动格式化代码 ...

  7. React中CodeMirror插件的使用及封装

    目录 一.CodeMirror是什么 二.React中CodeMirror的基本使用介绍 (一)引入CodeMirror 1. 安装CodeMirror插件 2.引入 CodeMirror 插件 (二 ...

  8. 适用于Mac和Windows的12种最佳WordPress编辑器的代码编辑器

    We often get asked about what's the best code editor for modifying WordPress files? Well you can use ...

  9. 常用的八款免费程序员喜欢的代码编辑器推荐「你用哪个」

    我们每个人心目中肯定有自己认为值得拥有的代码编辑器.作为程序员的你,平时使用哪款代码编辑器呢?作为我们工作效率工具,在这篇文章中准备整理8个常用且免费的代码编辑器.看看其中有没有你在用的这款.如果没有 ...

最新文章

  1. 大学的很重要的元素是圈子,是人气
  2. 采用URL访问资源,隐藏真实地址
  3. 接口 Closeable
  4. arcgis渔网分割提取栅格图_【操作】ArcGIS中字段的合并、分割、提取
  5. [摘]IIS上部署WCF的问题
  6. python pytest框架
  7. 数据结构(字符串)—— 删除“b“和“ac“
  8. 面试题汇总2(吐血整理)
  9. exosip2协议栈原理分析以及总结
  10. Python 之 解析xml
  11. 高级程序员与初级程序员差别在哪里?
  12. 这篇文章告诉你三个能给视频去水印的软件
  13. 天猫数据爬取解决找不到json文件的问题
  14. UnityHub无法打开项目:Failed to read D3DCompiler DLL file
  15. 再见!深圳!再见!腾讯!
  16. 自我介绍 的html页面,html初学者自我介绍网页
  17. 微波射频学习笔记22-------场效应管(MOS管)
  18. 51单片机Proteus仿真
  19. 5G NR 随机接入RACH流程(2)-- Msg1之生成PRACH Preamble
  20. 详解更改易语言的皮肤

热门文章

  1. 动态规划经典算法--最长公共子序列 LCS
  2. (原创) 看电影 源代码 有感——量子力学的玄妙
  3. 11 个重要的数据库设计规则
  4. ASP.NET MVC:缓存功能的设计及问题
  5. 配对交易方法_COVID下的自适应配对交易,一种强化学习方法
  6. 交叉验证python_交叉验证
  7. “去除更多的鲜艳色彩和动态效果的搭配,精简用户使用步长,让软件更像是一件工具。(不排除以后更先进的吸引眼球的方式)“。
  8. 鲶鱼效应:为什么要适当的贷款?
  9. Metadata Service 最高频的应用 - 每天5分钟玩转 OpenStack(164)
  10. Linux-Android启动之Machine-Init函数