vue中富文本框設置代碼高亮


1、安裝

npm install vue-quill-editor --save -dev // 富文本
npm install highlight.js --save -dev // 代碼高亮

2、在 main.js 中引入

import VueQuillEditor from 'vue-quill-editor'
import 'quill/dist/quill.core.css' // import styles
import 'quill/dist/quill.snow.css' // for snow theme
import 'quill/dist/quill.bubble.css' // for bubble theme

import 'highlight.js/styles/monokai-sublime.css'; // 代碼高亮樣式
Vue.use(VueQuillEditor)

3、在 需要使用富文本框的組件中

// html 代碼部分
<quill-editor v-model="wz.content" ref="myQuillEditor" :options="editorOption"></quill-editor>
// js
import hljs from 'highlight.js';

// data 數據部分
data(){ 
  return{ 
    content:'', 
    editorOption: {
       modules: { 
        toolbar: [
          ["bold", "italic", "underline", "strike"], 
          ["blockquote", "code-block"], 
          [{ header: 1 }, { header: 2 }], 
          [{ list: "ordered" }, { list: "bullet" }], 
          [{ script: "sub" }, { script: "super" }], 
          [{ indent: "-1" }, { indent: "+1" }], 
          [{ direction: "rtl" }], 
          [{ size: ["small", false, "large", "huge"] }], 
          [{ header: [1, 2, 3, 4, 5, 6, false] }], 
          [{ color: [] }, { background: [] }], 
          [{ font: [] }], [{ align: [] }], 
          ["clean"], ["link", "image", "video"]
                 ],
           syntax: { 
              highlight: text => { 
                  return hljs.highlightAuto(text).value; // 這里就是代碼高亮需要配置的地方 
                        }
                     }
                  }
               }
             }
         }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM