**
quillEditor 富文本的图片自定义上传
**
quill-Config.js
//axios相关配置,下面有相关文件
import service from '../../utils/request';
/*富文本编辑图片上传配置*/
const uploadConfig = {
action: '', // 必填参数 图片上传地址
methods: 'POST', // 必填参数 图片上传方式
headers: {
"Content-Type": "multipart/form-data"
},//请求头设置
token: '', // 可选参数 如果需要token验证,假设你的token有存放在sessionStorage
name: 'name', // 必填参数 文件的参数名
size: 10240, // 可选参数 图片大小,单位为Kb, 1M = 1024Kb
accept: 'image/png, image/gif, image/jpeg, image/bmp, image/x-icon,image/jpg' // 可选 可上传的图片格式
};
// toolbar工具栏的工具选项(默认展示全部)
const toolOptions = [
['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']
['link', 'image']
];
const handlers = {
image: function image() {
var self = this;
var fileInput = this.container.querySelector('input.ql-image[type=file]');
if (fileInput === null) {
fileInput = document.createElement('input');
fileInput.setAttribute('type', 'file');
// 设置图片参数名
if (uploadConfig.name) {
fileInput.setAttribute('files', uploadConfig.name);//参数名称
}
// 可设置上传图片的格式
fileInput.setAttribute('accept', uploadConfig.accept);
fileInput.classList.add('ql-image');
// 监听选择文件
fileInput.addEventListener('change', function () {
// 创建formData
let formData = new FormData();
formData.append('files', fileInput.files[0]);//这里的files需按需修改
fileInput.value = '';
//这里 请求
service.post(`上传地址`, formData, {
headers: {
"Content-Type": "multipart/form-data"
}
}).then(resp => {
//这里的根据上传成功返回值自定义修改
var path = resp.data[0].nCover;
//var picPath = process.env.FTP_URL + path;
let length = self.quill.getSelection(true).index;
//这里很重要,你图片上传成功后,img的src需要在这里添加,res.path就是你服务器返回的图片链接。
//console.log(path)
self.quill.insertEmbed(length, 'image', path);
self.quill.setSelection(length + 1)
fileInput.value = ''
});
});
this.container.appendChild(fileInput);
}
fileInput.click();
}
};
export default {
placeholder: '请输入内容',
theme: 'snow', // 主题
modules: {
toolbar: {
container: toolOptions, // 工具栏选项
handlers: handlers // 事件重写
}
}
};
引用
<template>
<!--
富文本中的内容,绑定的属性
v-model="updateProductDetailModel.introduction"
:options="editorOption"
富文本自定义的属性
-->
<quill-editor ref="myTextEditor"
v-model="updateProductDetailModel.introduction"
:options="editorOption"></quill-editor>
</template>
<script>
//富文本框
import 'quill/dist/quill.core.css';
import 'quill/dist/quill.snow.css';
import 'quill/dist/quill.bubble.css';
import { quillEditor } from 'vue-quill-editor';
import quillConfig from './quill-Config';
import Vue from 'vue';
export default {
data() {
return {
//自定义配置
editorOption: quillConfig,
updateProductDetailModel: {}
};
},
//组件,与data()平级
components: {
quillEditor
}
};
</script>
<style scoped>
</style>