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>
相关推荐
To_OC7 小时前
写了三遍 Todo List,我终于搞懂了 React 父子组件到底怎么通信
前端·javascript·react.js
勇往直前plus7 小时前
Vue3(篇一) 核心概念——响应式模板语法与组件基础
前端·javascript·vue.js
咩咩啃树皮7 小时前
第43篇:Vue3计算属性(computed)完全精讲——缓存机制、依赖计算、业务最优解
前端·vue.js·缓存
用户938515635079 小时前
从零搭建 AI 日记助手:用 Milvus 向量数据库 + RAG 让机器读懂你的每一天
javascript·人工智能·全栈
徐小夕9 小时前
花了一周,3亿tokens,我开源了一款 Word 文档智能审查平台,文稿自动质检+可视化分析,告别低效人工审核
前端·算法·github
a11177610 小时前
坦克大战3D Three.js 3D (开源项目)
开发语言·javascript·3d
kyriewen11 小时前
别再乱用useEffect了——你写的10个里有8个不该存在
前端·javascript·react.js
Ivanqhz11 小时前
Rust &‘static str浅析
java·前端·javascript·rust
IT_陈寒11 小时前
SpringBoot这个分页坑,我踩了三天才爬出来
前端·人工智能·后端
颜酱12 小时前
05 | 召回前置准备:根据业务数据库生成各数据库(读取配置阶段)
前端·人工智能·后端