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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
vue2quill是一个基于Vue.js和Quill.js实现的富文本编辑器组件。它支持上传图片和视频,但需要你自己实现上传的逻辑。 对于上传视频的实现,你可以使用一些常用的视频上传方案,比如通过后端接口实现文件上传、使用第三方云存储服务等。在vue2quill中,你需要做的就是通过配置参数来指定上传视频的接口地址或者上传视频的回调函数。 以下是一个使用vue2quill上传视频的示例代码: ``` <template> <div> <quill-editor v-model="content" :options="editorOption" @image-added="onImageAdded" @video-added="onVideoAdded"></quill-editor> </div> </template> <script> import QuillEditor from 'vue-quill-editor' export default { components: { QuillEditor }, data () { return { content: '', editorOption: { modules: { toolbar: [ ['bold', 'italic', 'underline', 'strike'], // toggled buttons ['blockquote', 'code-block'], [{ 'header': 1 }, { 'header': 2 }], // custom button values [{ 'list': 'ordered' }, { 'list': 'bullet' }], [{ 'script': 'sub' }, { 'script': 'super' }], // superscript/subscript [{ 'indent': '-1' }, { 'indent': '+1' }], // outdent/indent [{ 'direction': 'rtl' }], // text direction [{ 'size': ['small', false, 'large', 'huge'] }], // custom dropdown [{ 'header': [1, 2, 3, 4, 5, 6, false] }], [{ 'color': [] }, { 'background': [] }], // dropdown with defaults from theme [{ 'font': [] }], [{ 'align': [] }], ['clean'], // remove formatting button ['link', 'image', 'video'] // link and image, video ] }, placeholder: '请输入正文', theme: 'snow' } } }, methods: { onImageAdded (file, callback) { // 处理图片上传 let formData = new FormData() formData.append('file', file) axios.post('/upload/image', formData) .then(res => { callback(res.data.url) }) }, onVideoAdded (file, callback) { // 处理视频上传 let formData = new FormData() formData.append('file', file) axios.post('/upload/video', formData) .then(res => { callback(res.data.url) }) } } } </script> ``` 在上述代码中,我们通过配置QuillEditor组件的options参数来设置富文本编辑器的选项,包括工具栏、占位符等。在onImageAdded和onVideoAdded回调函数中,我们可以处理上传图片和上传视频的逻辑,并通过callback函数将上传后的图片或视频的地址返回给富文本编辑器组件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值