引用https://blog.csdn.net/weixin_42080056/article/details/98774354
1.安装vue-quill-editornpm i vue-quill-editor
2.引入import { quillEditor } from 'vue-quill-editor'
3.vue添加组件,隐藏element-ui的el-upload
<el-upload
v-show="false"
class="avatar-uploader"
action="七牛上传路径"
:data='fileUpload'
:show-file-list="false"
:on-success="uploadSuccess"
:before-upload="beforeUpload">
</el-upload>
<quill-editor
ref="myQuillEditor"
v-model="content"
:options="editorOption">
</quill-editor>
4.data
fileUpload:{
token:'',
key:''
},
content: '',
editorOption: {
placeholder: '请输入内容',
theme: 'snow',
modules: {
toolbar: {
container: [
['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': []}],
['link', 'image', 'video'],
['clean']],
handlers: {
'image': function (value) {
if (value) {
document.querySelector('.avatar-uploader input').click()
} else {
this.quill.format('image', false);
}
}
}
}
}
},
imgUrl:'',
5.重点是上传成功后的方法uploadSuccess,调用el-upload的上传功能,返回路径拼接图片插入富文本
uploadSuccess(res, file){
this.imgUrl = '七牛资源路径'+file.name
// 首先获取富文本编辑器的实例
let quill = this.$refs.myQuillEditor.quill
// 上传成功所执行
if (res) {
// 获取光标所在位置
let length = quill.getSelection().index;
// 插入图片res为服务器返回的数据
quill.insertEmbed(length, 'image', this.imgUrl)
// 光标移动至文本末端
quill.setSelection(length + 1)
} else {
this.$message.error('图片插入失败')
}
},
beforeUpload(file){
this.fileUpload.key = file.name
},
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。