VueQuill 富文本 快速上手开发全记录——附axios小坑 需手动重写axios以支持formdata对象

文章介绍了如何在Vue应用中使用@vueup/vue-quill富文本编辑器,包括安装、主文件引入、配置图片上传功能以及自定义toolbar。同时展示了处理图片上传的函数和axios接口请求,用于将编辑器内容提交到服务器。
摘要由CSDN通过智能技术生成

官网(需要魔法 全英文)https://vueup.github.io/vue-quill/guide/

安装下载npm install @vueup/vue-quill@beta

main.js引入

import { createApp } from 'vue'
import { QuillEditor } from '@vueup/vue-quill'
import '@vueup/vue-quill/dist/vue-quill.snow.css';

const app = createApp(App)
app.component('QuillEditor', QuillEditor)

配置文件参考

/* 富文本编辑图片上传配置*/
const uploadConfig = {
  // action: 'http://api.testing.xueping.com/workorder/upload/image', // 必填参数 图片上传地址
  // methods: 'POST', // 必填参数 图片上传方式
  // token: '', // 可选参数 如果需要token验证,假设你的token有存放在sessionStorage
  name: 'uploadFile', // 必填参数 文件的参数名
  size: 500, // 可选参数   图片大小,单位为Kb, 1M = 1024Kb
  accept: 'image/png, image/gif, image/jpeg, image/bmp, image/x-icon' // 可选 可上传的图片格式
}

// toolbar工具栏的工具选项(默认展示全部)
const toolOptions = [
  ['bold', 'italic', 'underline', 'strike'],
  ['blockquote', 'code-block'],
  // [{ 'header': 1 }, { 'header': 2 }],
  // [{ 'list': 'ordered' }, { 'list': 'bullet' }],
  // [{ 'script': 'sub' }, { 'script': 'super' }],
  // [{ 'indent': '-1' }, { 'indent': '+1' }],
  // [{ 'direction': 'rtl' }],
  // [{ 'size': ['small', false, 'large', 'huge'] }],
  [{ 'header': [1, 2, 3, 4, 5, 6, false] }],
  [{ 'color': [] }, { 'background': [] }],
  // [{ 'font': [] }],
  // [{ 'align': [] }],
  // ['clean'],
  ['link', 'image']
]
const handlers = {
  image: function image() {
    var self = this
    var fileInput = this.container.querySelector('input.ql-image[type=file]')
    if (fileInput === null) {
      fileInput = document.createElement('input')
      fileInput.setAttribute('type', 'file')
      // 设置图片参数名
      if (uploadConfig.name) {
        fileInput.setAttribute('name', uploadConfig.name)
      }
      // 可设置上传图片的格式
      fileInput.setAttribute('accept', uploadConfig.accept)
      fileInput.classList.add('ql-image')
      // 监听选择文件
      fileInput.addEventListener('change', function() {
        var formData = new FormData()
        formData.append(uploadConfig.name, fileInput.files[0])

        // 如果需要token且存在token
        if (uploadConfig.token) {
          formData.append('token', uploadConfig.token)
        }
        uploadPicture(formData).then(res=>{
          const length = self.quill.getSelection(true).index
          res.path = res.data.url
          self.quill.insertEmbed(length, 'image', res.path)
          self.quill.setSelection(length + 1)
        })
      })
      this.container.appendChild(fileInput)
    }
    fileInput.click()
  }
}
import { uploadPicture } from '../../http/index'
export default {
  placeholder: '',
  theme: 'snow', // 主题
  modules: {
    toolbar: {
      container: toolOptions, // 工具栏选项
      handlers: handlers // 事件重写
    }
  }
}

VUE组件内使用

<script>
import quillConfig from './quill-config'

const QuillEditor1 = ref(null)
function sendDemand() {
  QuillEditor1.value.getHTML()
  submitCommentLoading.value = true
  //接口请求与后期操作
}
</script>

<template>
<QuillEditor  :options="quillConfig"  ref="QuillEditor1"  class="quill-editor"   theme="snow" />
	<van-button  :loading="submitCommentLoading" size="small" type="primary" @click="sendDemand">	提交</van-button>
</template>

        axios({
          baseURL: import.meta.env.VITE_APP_BASE_API,
          timeout: 4*2000,
          method: "post",
          url: `upload/image`,
          data: getFormData(obj),
          headers: {  'Content-Type':'application/x-www-form-urlencoded', 
          // headers: {  'Content-Type':'application/json;text/plain', 
          //巨坑  本项目手动写死了JSON数据格式  formdata需要手动重新封装
                      'Authorization': `Bearer ${store.state.token}`}
        }).then(res=>{
            const length = self.quill.getSelection(true).index
            res.path = res.data.data.url
            self.quill.insertEmbed(length, 'image', res.path)
            self.quill.setSelection(length + 1)
        })
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

跳动的世界线

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值