vue-quill-editor上传图片vue3

编辑器配置图片上传,调用后端接口服务

看代码

复制代码
<quill-editor ref="quilRef" v-model:content="form.pushContent2" :options="editorOption"
                                    contentType="html" />


//隐藏上传图片
<input type="file" ref="fileInput" accept="image/*" style="display: none" @change="handleFileChange">






js部分



<script lang="ts" setup>
import axios from 'axios'
import { QuillEditor } from '@vueup/vue-quill';
import '@vueup/vue-quill/dist/vue-quill.snow.css'; // 引入样式文件

const fileInput = ref(null)
const uploadUrl = '你的上传接口路径'
const editorOption = {
    theme: 'snow',
    modules: {
        history: {
            delay: 0,
            maxStack: 0
        },
        toolbar: {
            container: [
                [{ 'size': ['small', false, 'large'] }],
                [{ 'color': [] }],
                [{ 'list': 'bullet' }],
                ['bold', 'italic', 'underline'],
                ['image', 'link']
            ],
            handlers: {
                image: () => fileInput.value.click()
            }
        }
    },// 自定义编辑器内的空白处理样式
    customOptions: {
        whitespace: 'pre-wrap'
    }
};

//上传图片
const handleFileChange = async (e) => {
    const file = e.target.files[0]
    if (!file) return

    try {
        const formData = new FormData()
        formData.append('file', file)
        formData.append('prefix', 'image')

        const res = await axios.post(uploadUrl, formData, {
            headers: { 'Content-Type': 'multipart/form-data' }
        })
        if (res.data.url) {
            insertImage(res.data.url)
        }
    } catch (error) {
        console.error('Upload failed:', error)
    } finally {
        e.target.value = '' // 重置input
    }
}
//向富文本插入图片
const insertImage = (url) => {
  const quill = quilRef.value.getQuill()
  const range = quill.getSelection()
  quill.insertEmbed(range.index, 'image', url)
  quill.setSelection(range.index + 1)
}
</script>
相关推荐
快起来别睡了13 分钟前
深入理解 Promise 的高阶用法:从入门到手写实现
前端
yvvvy18 分钟前
前端跨域全解析:从 CORS 到 postMessage,再到 WebSocket
前端·javascript·trae
摸着石头过河的石头1 小时前
手把手教你用VS Code玩转Gitee协作:从环境配置到高效提交
前端·visual studio code
张志鹏PHP全栈1 小时前
Vue3第十八天,Vue3中的组件通信
前端·vue.js
小周同学:2 小时前
在 Vue2 中使用 pdf.js + pdf-lib 实现 PDF 预览、手写签名、文字批注与高保真导出
开发语言·前端·javascript·vue.js·pdf
m0_494716682 小时前
CSS中实现一个三角形
前端·css
teeeeeeemo2 小时前
跨域及解决方案
开发语言·前端·javascript·笔记
JSON_L2 小时前
Vue Vant应用-数据懒加载
前端·javascript·vue.js
IT毕设实战小研3 小时前
基于Spring Boot校园二手交易平台系统设计与实现 二手交易系统 交易平台小程序
java·数据库·vue.js·spring boot·后端·小程序·课程设计