先安装vue-quill-editor
npm i vue-quill-editor --save
组件quillEditor代码 Editor.vue
<template>
<div class="quill" id="lpc-quill">
<el-upload v-show="false" ref="upload" :action="uploadFileUrl" class="upload-demo"
:on-success="upScuccess" :on-error="uploadError" />
<quill-editor ref="myQuillEditor" v-model="content" class="myQuillEditor" :options="editorOption" @change="$emit('input',content)" />
</div>
</template>
<script>
import {
quillEditor
} from 'vue-quill-editor'
import "quill/dist/quill.core.css";
import "quill/dist/quill.snow.css";
import "quill/dist/quill.bubble.css";
export default {
name: "QuillEdit",
components: {
quillEditor
},
props: {
/* 编辑器的内容 */
value: {
type: String,
default: "",
},
/* 高度 */
height: {
type: Number,
default: null,
},
/* 最小高度 */
minHeight: {
type: Number,
default: null,
},
/* 只读 */
readOnly: {
type: Boolean,
default: false,
}
},
data() {
return {
content:this.value,
currentValue: "",
uploadFileUrl: "https://图片上传路径", //上传的图片服务器地址
editContent: '',
editorOption: { // 富文本编辑器配置
placeholder: '请在这里输入',
modules: {
toolbar: {
container: [
["bold", "italic", "underline", "strike"], // 加粗 斜体 下划线 删除线
["blockquote", "code-block"], // 引用 代码块
[{
list: "ordered"
}, {
list: "bullet"
}], // 有序、无序列表
[{
indent: "-1"
}, {
indent: "+1"
}], // 缩进
[{
size: ["small", false, "large", "huge"]
}], // 字体大小
[{
header: [1, 2, 3, 4, 5, 6, false]
}], // 标题
[{
color: []
}, {
background: []
}], // 字体颜色、字体背景颜色
[{
align: []
}], // 对齐方式
["clean"], // 清除文本格式
["link", "image", "video"] // 链接、图片、视频
],
handlers: {
'image': function(value) {
if (value) {
document.querySelector('#lpc-quill .upload-demo input').click()
} else {
this.quill.format('image', false)
}
}
}
}
},
theme: 'snow'
}
}
},
computed: {
editor() {
return this.$refs.myQuillEditor.quill
},
styles() {
let style = {};
if (this.minHeight) {
style.minHeight = `${this.minHeight}px`;
}
if (this.height) {
style.height = `${this.height}px`;
}
return style;
},
},
created() {
this.editContent = this.value
},
watch: {
value:function(){
this.content = this.value;
}
},
methods: {
upScuccess(res, file) {
const quill = this.$refs.myQuillEditor.quill
if (res.code === 200 && res.data !== null) {
const length = quill.getSelection().index // 获取光标所在位置
quill.insertEmbed(length, 'image', res.data.url) // 插入图片
quill.setSelection(length + 1) // 调整光标位置到最后
} else {
this.$message.error('图片上传失败')
}
},
uploadError() {
this.$message.error('图片上传失败')
},
onEditorReady(editor) { // 准备编辑器
console.log(editor)
},
onEditorBlur() {}, // 失去焦点事件
onEditorFocus(val, editor) {}, // 获得焦点事件
onEditorChange() {
this.$emit('editorContent', this.editContent)
} // 内容改变事件
}
}
</script>
页面使用editor 组件 index.vue
<template>
<div>
<editor v-model="content" ref="myQuillEditor" />
</div>
</template>
<script>
import Editor from '@/components/Editor';
export default {
name: "index",
components: {
Editor,
},
data() {
return {
content:'',
}
}
}
</script>