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; // 這里就是代碼高亮需要配置的地方 } } } } } }