quill-editor+ElementUi 图片上传至服务器,返回图片路径

先安装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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值