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>  
相关推荐
特立独行的猫a3 小时前
uniapp-x的HarmonyOS鸿蒙应用开发:tabbar底部导航栏的实现
华为·uni-app·harmonyos·鸿蒙·uniapp-x
迪巴拉15253 小时前
基于Yolov8训练的Flask后端和Uniapp野生菌识别系统
yolo·flask·uni-app
biyezuopinvip18 小时前
基于Spring Boot的投资理财系统设计与实现(毕业论文)
java·spring boot·vue·毕业设计·论文·毕业论文·投资理财系统设计与实现
biyezuopinvip19 小时前
基于Spring Boot的投资理财系统设计与实现(任务书)
java·spring boot·vue·毕业设计·论文·任务书·投资理财系统设计与实现
huohuopro1 天前
Vue3 Webview 转 Android 虚拟导航栏遮挡问题记录
android·vue
码界筑梦坊2 天前
332-基于XGBoost与SHAP的可穿戴设备亚健康风险识别系统
python·数据分析·flask·vue·毕业设计
上单带刀不带妹2 天前
【Axios 实战】网络图片地址转 File 对象,附跨域解决方案
开发语言·前端·javascript·vue
SuperEugene2 天前
前端模块化与 import/export入门:从「乱成一团」到「清晰可维护」
前端·javascript·面试·vue
~央千澈~3 天前
优雅草正版授权系统 - 优雅草科技开源2月20日正式发布
python·vue·php·授权验证系统
Roc.Chang4 天前
Vite 启动报错:listen EACCES: permission denied 0.0.0.0:80 解决方案
linux·前端·vue·vite