vue项目页面嵌入代码块vue-prism-editor

news/2024/7/10 2:52:52 标签: vue, 代码高亮, 嵌入代码块

需求:

在这里插入图片描述
1. 可输入代码,并且代码语法高亮
2. 支持编辑和不可编辑模式
3. 提交到后端到代码内容为字符串格式

实现

在gitbug上找到vue-prism-editor,可以满足以上需求。

使用
  1. 安装vue-prism-editor

    npm install vue-prism-editor
    

    由于vue-prism-editor需要依赖 prismjs,所以还需要安装prismjs

    npm install prismjs
    
  2. 在需要使用vue-prism-editor的组件中引入

    import { PrismEditor } from "vue-prism-editor";
    import "vue-prism-editor/dist/prismeditor.min.css"; // import the styles somewhere
    
    // import highlighting library (you can use any library you want just return html string)
    import { highlight, languages } from "prismjs/components/prism-core";
    import "prismjs/components/prism-clike";
    import "prismjs/components/prism-javascript";
    import "prismjs/themes/prism-tomorrow.css"; // import syntax highlighting styles
    
  3. html代码

    <prism-editor
      class="my-editor height-300"
      v-model="code"
      :highlight="highlighter"
      :line-numbers="lineNumbers"
    ></prism-editor>
    

    code----为需要高亮显示的代码内容
    highlighter----定义在methods中的一个方法,用于把code高亮显示
    lineNumbers----是否显示行号
    readonly----是否

  4. js代码

    export default {
      components: {
        PrismEditor
      },
      data: () => ({
        code: 'console.log("Hello World")',
        lineNumbers: true, // true为编辑模式, false只展示不可编辑
      }),
      methods: {
        highlighter(code) {
          return highlight(code, languages.js); //returns html
        }
      }
    };
    
  5. css代码

    <style lang="scss">
    /* required class */
    .my-editor {
      background: #2d2d2d;
      color: #ccc;
    
      font-family: Fira code, Fira Mono, Consolas, Menlo, Courier, monospace;
      font-size: 14px;
      line-height: 1.5;
      padding: 5px;
    }
    
    /* optional */
    .prism-editor__textarea:focus {
      outline: none;
    }
    
    /* not required: */
    .height-300 {
      height: 300px;
    }
    </style>
    

注意: css样式必写,不然编辑器没有样式,只是纯白页面展示
“height-300” 是给编辑器设置高度的,高度可自行设置,也可以不设置,这个样式非必需

到这里,功能基本就实现了。
但是在过程中遇到一些问题,这里也一并总结。

问题

  1. 如果仅安装vue-prism-editor,没有安装prismjs,会报以下错误,npm install prismjs即可
    在这里插入图片描述

  2. 如果报错中有提示升级或者安装ajv或者vue2.6.X版本,根据提示安装即可

    npm install ajv@^6.9.1
    npm install vue@^2.6.11
    

    个人理解,如果ajv和vue版本过低,可能会导致vue-prism-editor依赖的相关东西安装不上,建议升级完ajv和vue之后,再重新安装vue-prism-editor和prismjs.

  3. vuevue-template-compiler版本不一致
    在这里插入图片描述
    卸载低版本vue-template-compiler

    npm uninstall vue-template-compiler
    

    然后安装跟vue同样版本的vue-template-compiler

    npm install vue-template-compiler@2.6.11
    

http://www.niftyadmin.cn/n/534449.html

相关文章

惊人之鸣(2006.12.10)

惊人之鸣 -------------------- 楚庄怪鸟三载鸣&#xff0c; 昏庸冷面窥众臣。中原问鼎宋事多&#xff0c;君子忠心霸业成。

AJAX请求 $.post方法的使用

使用jQuery的$.post方法可以以POST形式向服务器发起AJAX请求。$.post方法是jQuery的实用工具方法。 $.post方法语法 $.post(url,parameters,callback) 参数 url (字符串)服务器端资源地址。 parameter (对象)需要传递到服务器端的参数。 参数形式为“键/值”。 callback…

中庸之士(2007.04.13)

中庸之士 -----------------------少语慎言实事间&#xff0c;跬步垒筑人品苷。维心维物皆知涣&#xff0c;唯有吾辈非汉颜。

【vue】vue-devtools安装使用

vue-devtools安装使用安装配置使用安装配置 github下载安装包 https://github.com/vuejs/vue-devtools 在这里插入图片描述 yarn配置devtools&#xff0c;使用npm配置遇到一些问题&#xff0c;最后还是决定使用yarn配置 若没有安装yarn&#xff0c;首先全局安装yarn npm inst…

LeetCode Zigzag Iterator

原题链接在这里&#xff1a;https://leetcode.com/problems/zigzag-iterator/ 题目&#xff1a; Given two 1d vectors, implement an iterator to return their elements alternately. For example, given two 1d vectors: v1 [1, 2] v2 [3, 4, 5, 6] By calling next repea…

一生受用的格言

成功者找机会突破&#xff0c;失败者找借口抱怨&#xff1b; 习惯于找借口&#xff0c;是一种不负责任的表现&#xff1b; 想改变自己命运的时候&#xff0c;从态度开始&#xff1b; 用放大镜对待别人的优点&#xff1b; 用望远镜看待别人的缺点。

匹配内容高亮

需求&#xff1a; 通过正则表达式匹配一段内容&#xff0c;匹配到的部分在原内容上高亮显示&#xff0c;实现效果如下&#xff1a; 实现方式&#xff1a; 拿到匹配到的内容&#xff0c;给匹配到的内容加上标签样式去替换原文本里的这段内容&#xff1b; /** * matches是一…

POJ 3537 Crosses and Crosses(SG/还未想完全通的一道SG)

题目链接 #include<iostream> #include<cstdio> #include<cstring> using namespace std;int sg[2010];int get_sg(int n) {if(n<0) return 0;if(sg[n]!-1) return sg[n];bool vis[2010];//莫名其妙&#xff01;//vis[]数组要声明在函数里&#xff0c;如果…