vue-quill-editor 富文本编辑器上传图片所遇到的坑

最近在后台管理系统使用vue-quill-editor 富文本编辑器所遇到的坑

1.当上传大图片时,图片资源会被转成base64格式,当图片过大时字符串的大小可能达到2M以上,数据库无法保存。

2…vue-quill-editor编辑器在一个页面使用多个富文本框上传图片时。只能获取到第一个富文本实例,

解决方案:
1.vue-quill-editor提供了一个 handlers 可以自定义图片上传的方式,我们可以结合element ui 的上传 组件自定义图片上传
2.当一个页面存在多个富文本编辑器的时候 ref不能写死

ref="toref"

代码

<div>
    <el-upload
      class="upload-demo"
      action="#"
      name="file"
      :http-request="requestUpload"
      :before-upload="beforeAvatarUpload"
      :on-success="handleSuccess"
      multiple
    />
    <quill-editor
      :ref="toref"
      v-model="content"
      @change="$emit('input', content)"
      :options="editorOption"
      style="height:400px;margin-bottom: 80px"
    ></quill-editor>
  </div>
</template>

参数

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
];

components: {
    quillEditor
  },
  props: {
    value: {
      type: String
    },
    toref: {
      type: String,
      default: "quillEditor"
    },
    quillIndex: {
      type: Number,
      default: 0
    }
  },
  watch: {
    value: function() {
      this.content = this.value;
    }
  },
  data() {
    return {
      quillIndex1:1,
      content: this.value,
      editorOption: {
        placeholder: "编辑内容",
        modules: {
          toolbar: {
            container: toolbarOptions,
            handlers: {
              image: (value) =>{
                if (value) {
                  const index = this.quillIndex;
                  // 这是是为了获取你当前点击的那个富文本框
                  document.querySelectorAll(".upload-demo input")[index].click();
                } else {
                  this.quill.format("image", false);
                }
              }
            }
          }
        }
      }
    };
  },

方法

//覆盖默认的上传行为
    requestUpload(params) {
      let formData = new FormData();
      formData.append("file", params.file);
      uploadFile(formData).then(res => {
        params.onSuccess(res);
        this.msgSuccess("上传成功");
      });
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg " || "image/png";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传图片只能是 JPG , PNG格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    handleSuccess(res, file) {
      //alert(JSON.stringify(res));
      //获取富文本组件实例
      // let quill = this.$refs.myTextEditor.quill;
      //如果上传成功
      if (res) {
      	//动态添加 ref  通过 eval () 去执行
        let toeval = "this.$refs." + this.toref + ".quill";
        //eval() 函数可计算某个字符串,并执行其中的的 JavaScript 代码。
        let quill = eval(toeval);
        console.log(quill);
        //获取光标所在位置
        let length = quill.selection.savedRange.index;
        // 插入图片,res为服务器返回的图片链接地址
        quill.insertEmbed(length, "image", res.url);
        // 调整光标到最后
        quill.setSelection(length + 1);
      } else {
        // 提示信息,需引入Message
        this.$message.error("图片插入失败");
      }
      //this.form.unscramble = res.fileName;
    }
  }

页面上使用vue-quill-editor封装成的组件。

<new-editor v-model="form.scheduling" :quillIndex="3" toref="Editorc3" ></new-editor>
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值