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>  
相关推荐
AI3D_WebEngineer1 天前
企业级业务平台项目设计、架构、业务全解之组件库篇
前端·javascript·vue
00后程序员张1 天前
iOS 上架费用全解析 开发者账号、App 审核、工具使用与开心上架(Appuploader)免 Mac 成本优化指南
android·macos·ios·小程序·uni-app·cocoa·iphone
木子李BLOG1 天前
vue.js设计与实现(待续)
vue
前端与小赵1 天前
uni-app开发安卓app时控制屏幕常亮不息屏
android·gitee·uni-app
2501_916008892 天前
HTTPS 请求抓包,从原理到落地排查的工程化指南(Charles / tcpdump / Wireshark / Sniffmaster)
ios·小程序·https·uni-app·wireshark·iphone·tcpdump
xiaohe06012 天前
🥳 Uni ECharts 2.1 发布:正式支持鸿蒙,零成本迁移、全平台兼容、跨端开发零负担!
vue.js·uni-app·echarts
悟能不能悟2 天前
reason: unable to verify the first certificate 如何处理
vue
2501_915909062 天前
WebView 调试工具全解析,解决“看不见的移动端问题”
android·ios·小程序·https·uni-app·iphone·webview
!win !2 天前
通过重写组件轻松掌握用JSX写Vue项目
vue·jsx
2501_915106322 天前
App 怎么上架 iOS?从准备资料到开心上架(Appuploader)免 Mac 上传的完整实战流程指南
android·macos·ios·小程序·uni-app·iphone·webview