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

官网(需要魔法 全英文)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)
        })

猜你喜欢

转载自blog.csdn.net/Beatingworldline/article/details/129861624