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>  
相关推荐
草根大哥1 小时前
AI编程实践-homex物业管理平台(Go + Vue3 + MySQL 多租户落地)
mysql·golang·vue·ai编程·gin·物业管理系统·多租户
予你@。1 小时前
uni-app(Vue3)实现自定义 Tab 切换滑块效果(微信小程序)
vue.js·微信小程序·uni-app
HashTang16 小时前
【AI 编程实战】第 11 篇:让小程序飞起来 - 性能优化实战指南
前端·uni-app·ai编程
lruri1 天前
记录一个修复nvue文件在vscode里面提示ts-plugin报错
uni-app
蓝帆傲亦1 天前
Web前端Mock数据实战指南:正确使用Mock.js提升开发效率
微信小程序·小程序·uni-app
00后程序员张1 天前
iOS 应用代码混淆,对已编译 IPA 进行类与方法混淆
android·ios·小程序·https·uni-app·iphone·webview
未来龙皇小蓝1 天前
RBAC前端架构-03:创建dotenv及config配置
前端·vue
木子啊1 天前
Uni-app社会化功能:登录支付分享全攻略
uni-app
笨笨狗吞噬者2 天前
【2025】加入 uniapp 的一年
前端·uni-app
2501_915918412 天前
mobileprovision 描述文件内容查看,查看 Bundle ID、证书指纹等来定位安装与签名问题
android·ios·小程序·https·uni-app·iphone·webview