uniapp+vue3+ts H5端使用Quill富文本插件以及解决上传图片反显的问题

uniapp+vue3+ts H5端使用Quill富文本插件以及解决上传图片反显的问题

1.在项目中安装Quill

javascript 复制代码
npm i quill@1.3.7

2.需要显示富文本的页面完整代码

javascript 复制代码
<template>
  <view>
  	 <div ref="quillEditor" style="height: 65vh"></div>
   </view>
</template>	 
<script setup lang="ts">
import { ref, onMounted, reactive } from 'vue'
import Quill from 'quill'
import 'quill/dist/quill.snow.css'

const quillEditor = ref(null)
let quill: any = null
const toolbarOptions = ref([
    [{ header: [1, 2, 3, 4, 5, 6, false] }],
    ['bold', 'italic', 'underline'], // toggled buttons
    ['image'],
    [{ align: [] }]
])

onMounted(() => {
    getQuill()
})
// 初始化编辑器
const getQuill = () => {
    quill = new Quill(quillEditor.value, {
        theme: 'snow',
        modules: {
            toolbar: toolbarOptions.value
        }
    })
    // 监听工具栏的图片按钮点击事件
    quill.getModule('toolbar').addHandler('image', handleImageUpload)
}
// 图片上传
    const uploadImage = async (filePath: string): Promise<string> => {
      try {
        const [uploadResult] = await uni.uploadFile({
          url: 'xxxxxxxxxxx', // 替换为实际的上传接口地址
          filePath,
          name: 'file',
          formData: {},
        })
        const response = JSON.parse(uploadResult.data)
        if (response.code === 200) {
          return response.data.url // 假设返回的数据中包含图片的完整 URL
        } else {
          throw new Error(response.message || '图片上传失败')
        }
      } catch (error) {
        console.error('上传图片出错:', error)
        throw error
      }
    }

// 替换 Quill 默认的图片处理逻辑
const handleImageUpload = () => {
    uni.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'], // 原图或压缩图
        sourceType: ['album', 'camera'], // 相册或相机
        success: async (res) => {
            const filePath = res.tempFilePaths[0]
            try {
                const data: any = await uploadImage(filePath)
                const range = quill.getSelection()
                if (range) {
                    quill.insertEmbed(range.index, 'image', data.absolute_path)
                }
            } catch (error) {
                uni.showToast({
                    title: '图片上传失败',
                    icon: 'none'
                })
            }
        },
        fail: (err) => {
            console.error('选择图片失败:', err)
        }
    })
}

// 获取编辑器内容
const getContent = () => {
    if (quill) {
        const content = quill.root.innerHTML
        form.answer_detail = content
        console.log(content) // 这里可以获取到Quill编辑器的HTML内容
    }
}
</script>  
相关推荐
街尾杂货店&25 分钟前
word属性解释
uni-app
java水泥工8 小时前
酒店客房管理系统|基于SpringBoot和Vue的酒店客房管理系统(源码+数据库+文档)
spring boot·vue·酒店管理系统·酒店客房管理系统
笨笨狗吞噬者10 小时前
【uniapp】小程序体积优化,JSON文件压缩
前端·微信小程序·uni-app
源码集结号12 小时前
一套智慧工地云平台源码,支持监管端、项目管理端,Java+Spring Cloud +UniApp +MySql技术开发
java·mysql·spring cloud·uni-app·源码·智慧工地·成品系统
木易 士心18 小时前
Uni-App 实现多身份动态切换 TabBar 指南
uni-app
2501_915106321 天前
CDN 可以实现 HTTPS 吗?实战要点、部署模式与真机验证流程
网络协议·http·ios·小程序·https·uni-app·iphone
爱看书的小沐2 天前
【小沐学WebGIS】基于Three.JS绘制飞行轨迹Flight Tracker(Three.JS/ vue / react / WebGL)
javascript·vue·webgl·three.js·航班·航迹·飞行轨迹
LoveEate2 天前
uniapp 运行/发版微信小程序
微信小程序·小程序·uni-app
fakaifa3 天前
【高级版】沃德政务招商系统源码+uniapp小程序
小程序·uni-app·源码下载·沃德政务招商系统·招商系统源码
weixin_446938873 天前
uniapp vue-i18n如何使用
前端·vue.js·uni-app