1.用之前首先需要安装
npm i vue-quill-editor --save
npm i quill --save
2.注册
入口文件main.js
import VueQuillEditor from 'vue-quill-editor'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import * as Quill from 'quill'
Vue.use(VueQuillEditor)
3.template内使用
<div class="edit_wrap">
<quill-editor v-model="content" ref="quillEditor" :options="editorOption" @blur="onBlur($event)"
@focus="onFocus($event)" @change="onChange($event)">
</quill-editor>
<button v-on:click="save">保存</button>
</div>
配置
4.js部分
data(){
return {
content:'请输入编辑内容',
editorOption: {
modules: {
toolbar: [
['bold', 'italic', 'underline', 'strike'], //加粗,斜体,下划线,删除线
['blockquote', 'code-block'], //引用,代码块
[{ 'header': 1 }, { 'header': 2}], // 标题,键值对的形式;1、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'], //清除字体样式
['image', 'video'] //上传图片、上传视频
],
},
theme: 'snow',
},
}
},
computed: {
editor() {
return this.$refs.quillEditor.quill;
},
},
methods:{
onBlur() {}, // 失去焦点事件
onFocus() {}, // 获得焦点事件
onChange() {}, // 内容改变事件
save(event) {
alert(this.content);
},
}
当然这还不行,这样虽然能编辑东西但是编辑的内容样式不生效,需要在显示显示文本v-html的地方加上对应的class ,具体如下:
<div class="ql-container ql-snow">
<div class="ql-editor" v-html="content"></div>
</div>
即内容元素的父级加class=“ql-container ql-snow”,v-html元素上加 class=“ql-editor” 就可以了