步骤1:使用npm 安装vue-quill-editor
npm install vue-quill-editor --save
当前安装的的版本是 "vue-quill-editor": "^3.0.6"
该插件是基于 Quill、适用于 Vue 的富文本编辑器,支持服务端渲染和单页应用。故在安装vue-quill-editor的同时也按照了Qill模块,可以在node_modules文件夹中进行查看
vue-quill-editor 文档: vue-quill-editor docs link
Qill api文档: Qill api docs link
步骤2:局部引入组件
<template>
<div class="section-editor">
<quillEditor v-model="content" ref="vueQuillEditor" :options="editorOption"
@blur="onEditorBlur($event)" @focus="onEditorFocus($event)"@change="onEditorChange($event)">
</quillEditor>
</div>
</template>
<script>
import { quillEditor } from "vue-quill-editor"; //调用编辑器
// 引入样式,此时样式是直接从quill文件中直接引入
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
export default {
name: 'editor',
components: { quillEditor },
data() {
return {
content: null,
editorOption: {}
}
},
methods: {
onEditorBlur(){}, // 失去焦点事件
onEditorFocus(){}, // 获得焦点事件
onEditorChange(){}, // 内容改变事件
},
computed: {
editor() {
return this.$refs.vueQuillEditor.quill;
}
}
}
</script>
全局引入【在main.js文件中】
import QuillEditor from "vue-quill-editor";
// 引入样式
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
Vue.use(QuillEditor)
//调用
<quill-editor v-model="content" ref="vueQuillEditor" :options="editorOption"
@blur="onEditorBlur($event)" @focus="onEditorFocus($event)" @change="onEditorChange($event)"> </quill-editor>
定义工具栏:【法一】通过options传值,下列配置为插件默认值:
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']
]
}
}
【法二】根据id自己定义工具栏内容
editorOption: {
modules: {
toolbar: '#toolbar'
}
}
工具栏插槽如下:
<quill-editor v-model="content" ref="vueQuillEditor" :options="editorOption"
@blur="onEditorBlur($event)" @focus="onEditorFocus($event)" @change="onEditorChange($event)">
<div id="toolbar" slot="toolbar">
<!-- Add a bold button -->
<button class="ql-bold">Bold</button>
<button class="ql-italic">Italic</button>
<!-- Add font size dropdown -->
<select class="ql-size">
<option value="small"></option>
<!-- Note a missing, thus falsy value, is used to reset to default -->
<option selected></option>
<option value="large"></option>
<option value="huge"></option>
</select>
<select class="ql-font">
<option selected="selected"></option>
<option value="serif"></option>
<option value="monospace"></option>
</select>
<!-- Add subscript and superscript buttons -->
<button class="ql-script" value="sub"></button>
<button class="ql-script" value="super"></button>
<!-- You can also add your own -->
<button id="custom-button" >[ Click me ]</button>
</div>
</quill-editor>
小棨留言:文章描述或者语法理解不到位的地方,请大家多多指教!