vue-quill-editor图片上传到服务器

<template>
  <div>
    <!-- // 文件上传组件 -->
    <el-upload
      id="upimg"
      v-show="false"
      class="upload-demo"
      :action="imgUploadUrl"
      :on-success="handleSuccess"
      multiple
    >
      <el-button size="small" type="primary">点击上传</el-button>
    </el-upload>

    <!-- // 富文本编辑器组件 -->
    <quill-editor v-model="content" :options="editorOption" ref="QuillEditor">
    </quill-editor>
  </div>
</template>
<script>
// require styles 引入样式
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'

// 富文本编辑器工具栏配置,因为涉及到富文本编辑器以外的组件调用,所以在这里我们需要重新自定义一下富文本编辑器的工具栏
const toolbarOptions = [
  ['bold', 'italic', 'underline', 'strike'], // toggled buttons
  ['blockquote', 'code-block'],

  [{ header: 1 }, { header: 2 }], // custom button values
  [{ list: 'ordered' }, { list: 'bullet' }],
  [{ script: 'sub' }, { script: 'super' }], // superscript/subscript
  [{ indent: '-1' }, { indent: '+1' }], // outdent/indent
  [{ direction: 'rtl' }], // text direction

  [{ size: ['small', false, 'large', 'huge'] }], // custom dropdown
  [{ header: [1, 2, 3, 4, 5, 6, false] }],

  [{ color: [] }, { background: [] }], // dropdown with defaults from theme
  [{ font: [] }],
  [{ align: [] }],
  ['link', 'image', 'video'],
  ['clean'] // remove formatting button
]

export default {
  components: {
    quillEditor
  },
  data() {
    return {
      // 上传图片接口地址
      imgUploadUrl: 'http://xxxxxxxx/fileUpload',
      // 富文本编辑器内容
      content: '',
      // 富文本编辑器工具栏
      editorOption: {
        modules: {
          toolbar: {
            container: toolbarOptions, // 工具栏
            handlers: {
              image: function (value) {
                if (value) {
                  // 调用element的图片上传组件
                  // (这里是用的原生js的选择dom方法)
                  document.querySelector('#upimg button').click()
                } else {
                  this.quill.format('image', false)
                }
              }
            }
          }
        }
      }
    }
  },
  methods: {
    // element的upload组件上传图片成功后调用的函数
    handleSuccess(res) {
      // 获取富文本组件实例
      const quill = this.$refs.QuillEditor.quill
      if (res) {
        // 如果上传成功
        // 获取光标所在位置
        const length = quill.getSelection().index
        // 插入图片,res为服务器返回的图片链接地址
        quill.insertEmbed(length, 'image', res.data)
        // 调整光标到最后
        quill.setSelection(length + 1)
      } else {
        // 提示信息
        this.$message.error('图片插入失败')
      }
    }
  }
}
</script>

<style></style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值