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>  
相关推荐
郑州光合科技余经理2 小时前
代驾系统架构拆解:订单链路、权限组织与私有化源码交付
开发语言·后端·算法·架构·系统架构·uni-app·php
FeelTouch Labs12 小时前
Node.js 中的 spawn 和 exec:子进程执行的不同方式对比
node.js·编辑器·vue·vim
shawxlee16 小时前
vue3+elementplus+sass/scss安装配置教程:使用svg图标、引入外部字体、定义变量模块、修改组件样式、一键切换主题等
vue·sass·svg·字体·element·scss·主题样式
弹简特19 小时前
【Vue3速成】07-vue3异步请求之Axios-01
vue·axios
2501_9159214319 小时前
详细解析,iOS 应用上架 App Store 的完整流程与指南
android·ios·小程序·https·uni-app·iphone·webview
2501_9159184120 小时前
Xcode调试iOS应用内存管理与优化,识别泄漏与性能监控指南
android·ios·小程序·uni-app·cocoa·iphone·xcode
波罗丁牌1 天前
uni-app 实现视频聊天、屏幕分享,支持Android、HarmonyOS、iOS
android·uni-app·音视频
前端l2 天前
uni‑app(Vue3+setup)开发微信小程序高频坑汇总
微信·uni-app
pan3035074792 天前
uniapp 不用发版也可以做到版本更新
uni-app
00后程序员张2 天前
使用Instruments工具深入分析iOS应用性能与启动时间优化
android·macos·ios·小程序·uni-app·cocoa·iphone