【Vue】运行npm run dev出现98%vue-quill-editor 富文本编辑器错误解决

本文介绍了如何在Vue应用中安装并使用VUE-Quill-Editor富文本编辑器,包括下载、全局引入、配置选项和处理图片上传。作者详细展示了配置工具栏和图片上传接口的步骤,以及图片上传前后的处理逻辑。
摘要由CSDN通过智能技术生成

感谢:SpiritualTuto,来自:VUE vue-quill-editor 富文本编辑器的使用_npm install --save quill/dist/quill.bubble.css qui-CSDN博客

1、下载Vue-Quill-Editor

npm install vue-quill-editor --save


2、如果还不行,下载quill(Vue-Quill-Editor需要依赖)

npm install quill --save


3、全局引入

     import 'quill/dist/quill.core.css'
     import 'quill/dist/quill.snow.css'
     import 'quill/dist/quill.bubble.css'
     import { quillEditor } from 'vue-quill-editor
     //单独页面注册一下
     exp def{
         components: {quillEditor},
     }
  <quill-editor
      ref="newEditor"
      v-model="content"
      class="qediter"
      :style="`height: ${height}px`"
      :options="editorOption"
      @change="onEditorBlur($event)"
    />

4、** 选项配置(如果为空的就默认展示所有选项)
 

   editorOption: {
        placeholder: '请在这里输入',
        modules: {
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'], //加粗,斜体,下划线,删除线
            ['blockquote', 'code-block'], //引用,代码块
            [{ header: 1 }, { header: 2 }], // 标题,键值对的形式;1、2表示字体大小
            [{ list: 'ordered' }, { list: 'bullet' }], //列表
            [{ indent: '-1' }, { indent: '+1' }], // 缩进
            [{ direction: 'rtl' }], // 文本方向
            [{ size: ['small', false, 'large', 'huge'] }], // 字体大小
            [{ header: [1, 2, 3, 4, 5, 6, false] }], //几级标题
            [{ color: [] }, { background: [] }], // 字体颜色,字体背景颜色
            [{ align: [] }], //对齐方式
            ['clean'], //清除字体样式
            ['image', 'video'] //上传图片、上传视频
          ]
        }
      },


此时此刻 你就可以实现最基础的富文本编辑器啦~

但是富文本编辑器上传图片的格式为 base64位的 , 后端无法处理

于是我们要调取后端的接口获取地址, 并且要把图片反显到编辑器上

body部分

<template>
  <div class="container">
    <quill-editor
      ref="newEditor"
      v-model="content"
      class="qediter"
      :style="`height: ${height}px`"
      :options="editorOption"
      @change="onEditorBlur($event)"
    />
    <!-- 隐藏upload 上传图片 -->
    <el-upload
      ref="uploadImg"
      class="upload-img"
      //这里写入你的接口地址
      action="#########"
      :before-upload="picBeforeupload"
      :on-error="picError"
      :on-success="success"
      accept="image/png, image/jpeg, image/jpg, image/gif"
      :show-file-list="false"
    >
      <slot name="trigger">
        <div id="editorUploadImage" />
      </slot>
    </el-upload>
  </div>
</template>

JS部分

 props: {
    value: {
      type: String,
      default: ''
    },
    option: {
      type: Object,
      default: () => ({})
    },
    height: {
      type: Number,
      default: 500
    }
  },
  data() {
    return {
      content: '',
      editorOption: {
        placeholder: '请在这里输入',
        modules: {
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'], //加粗,斜体,下划线,删除线
            ['blockquote', 'code-block'], //引用,代码块
            [{ header: 1 }, { header: 2 }], // 标题,键值对的形式;1、2表示字体大小
            [{ list: 'ordered' }, { list: 'bullet' }], //列表
            [{ indent: '-1' }, { indent: '+1' }], // 缩进
            [{ direction: 'rtl' }], // 文本方向
            [{ size: ['small', false, 'large', 'huge'] }], // 字体大小
            [{ header: [1, 2, 3, 4, 5, 6, false] }], //几级标题
            [{ color: [] }, { background: [] }], // 字体颜色,字体背景颜色
            [{ align: [] }], //对齐方式
            ['clean'], //清除字体样式
            ['image', 'video'] //上传图片、上传视频
          ]
        }
      },
      addImgRange: null
    }
  },
  watch: {
    value: {
      handler(newValue, preValue) {
        if (newValue !== preValue && newValue !== this.content) {
          this.content = newValue
        }
      },
      immediate: true
    }
  },
  created() {
    Object.assign(this.editorOption, this.option)
  },
  mounted() {
    this.init()
      },
  methods: {
    //成功回调
    success(res){
      const url = Api.imgUrl +res.data
       this.addImg(url)
    },
    init() {
      // 重写图片添加图片
      const imgHandler = state => {
        if (state) {
          document.getElementById('editorUploadImage').click()
        }
      }
      this.$refs.newEditor.quill.getModule('toolbar').addHandler('image', imgHandler)
    },

    onEditorBlur() {
      this.$emit('input', this.content)
    },
    // 图片大小检查
    picBeforeupload(file) {
      const isLt4M = file.size / 1024 / 1024 < 4
      if (!isLt4M) {
        this.$message.error('上传图片大小不能超过 4MB!')
      }
      return isLt4M
    },
    // 上传图片失败
    picError() {
      this.$message({
        message: '图片添加失败,请重试',
        type: 'error'
      })
    },
    // 添加图片
    addImg(imgUrl) {
      this.addImgRange = this.$refs.newEditor.quill.getSelection() // 检索用户的选择范围, 如果编辑没有焦点,可能会返回一个null
      this.$refs.newEditor.quill.insertEmbed(
      this.addImgRange != null ? this.addImgRange.index : 0,
      'image',
       imgUrl,
      )
    }
  }

  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

敦厚的曹操

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

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

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

打赏作者

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

抵扣说明:

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

余额充值