vue+el-upload+vue-quill-editor富文本编辑器(同一页面多个编辑器)

先看效果

第一步:

npm install vue-quill-editor --save

第二步:

import { quillEditor } from 'vue-quill-editor'

import 'quill/dist/quill.core.css'

import 'quill/dist/quill.snow.css'

import 'quill/dist/quill.bubble.css'

如上三个css必引入,否则页面样式会让你惊掉下巴

第三步:

 第四步

<el-form :model="form" status-icon ref="ruleForm" label-width="160px" @submit.native.prevent :rules="rules">
 <el-form-item label="考试内容" prop="content">
          <div class="navone">
            <div v-loading="quillUpdateImg" element-loading-text="请稍等,图片上传中">
              <el-upload class="avatar-uploader" :action="serverUrl" name="file" :headers="header" :show-file-list="false" :data="uploadData" :on-success="uploadSuccess" :on-error="uploadError"
                :before-upload="beforeUpload">
              </el-upload>
              <quill-editor class="editor" v-model="form.content" ref="content" :options="editorOption" @focus="onEditorFocus('content')">
              </quill-editor>
            </div>
          </div>
        </el-form-item>
 <el-form-item label="考试费用" prop="cost">
          <div class="navone">
            <div v-loading="quillUpdateImg" element-loading-text="请稍等,图片上传中">
              <el-upload class="avatar-uploader" :action="serverUrl" name="file" :headers="header" :show-file-list="false" :data="uploadData" :on-success="uploadSuccess" :on-error="uploadError"
                :before-upload="beforeUpload">
              </el-upload>
              <quill-editor class="editor" v-model="form.cost" ref="cost" :options="editorOption" @focus="onEditorFocus('cost')">
              </quill-editor>
            </div>
          </div>
        </el-form-item>
</el-form>

 

// 工具栏配置
const toolbarOptions = [
  ['bold', 'italic', 'underline', 'strike'], // 加粗 斜体 下划线 删除线
  ['blockquote', 'code-block'], // 引用  代码块
  [{ header: 1 }, { header: 2 }], // 1、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', 'video'], // 链接、图片、视频
]
export default {
    components:{quillEditor},
     data() {
        return {
             content: '',
             cost: '',
                
              uploadData: {}, // 图片文件
              serverUrl: '', // 这里写你要上传的图片服务器地址  * post接口
              header: {
                    // 有的图片服务器要求请求头需要有token,有的可以加上
                    Authorization: 'Bearer ',
              },
      quillUpdateImg: false, // 根据图片上传状态来确定是否显示loading动画,刚开始是false,不显示
      editorOption: {
        placeholder: '',
        theme: 'snow', // or 'bubble'
        placeholder: '请输入文章内容',
        modules: {
          toolbar: {
            container: toolbarOptions,
            // container: "#toolbar",
            handlers: {
              image: function (value) {
                if (value) {
                  document.querySelector('.avatar-uploader input').click()
                } else {
                  this.quill.format('image', false)
                }
              },
              link: function (value) {
                if (value) {
                  var href = prompt('注意!只支持公众号图文链接')
                  this.quill.format('link', href)
                } else {
                  this.quill.format('link', false)
                }
              },
            },
          },
        },
      },
      editor_ref: '',
        }
     }
}

 下面是methods

   onEditorBlur(editor) {
      console.log(this.form)
    },
    onEditorFocus(editor_ref) {
      this.editor_ref = editor_ref
    },
    onEditorChange(editor) {},
    beforeUpload() {
      // this.quillUpdateImg = true
    },
    uploadSuccess(res, file) {
      // 获取富文本组件实例
      var ref_el = this.$refs[this.editor_ref]
      console.log(ref_el)
      let quill = this.$refs[this.editor_ref].quill
      // 如果上传成功
      if (res.code == 0 && res.result.url) {
        // 获取光标所在位置
        let length = quill.getSelection().index
        // 插入图片  res.info为服务器返回的图片地址
        quill.insertEmbed(length, 'image', res.result.url)
        // 调整光标到最后
        quill.setSelection(length + 1)
      } else {
        this.$message.error('图片插入失败')
      }
      // this.quillUpdateImg = false
    },
    uploadError() {
      // this.quillUpdateImg = false
      this.$message.error('图片插入失败')
    },

onEditorFocus、uploadSuccess是关键函数

同一页面多个编辑器的实现:

无意间发现点击编辑器上的图片上传按钮,可以触发onEditorFocus方法,所以就用这个函数来区分当前正在操作的编辑器的ref,这就是为什么uploadSuccess函数里面这么写的理由了

// 根据this.editor_ref获取当前正在操作的编辑器
let quill = this.$refs[this.editor_ref].quill

 到这里就结束啦~


自己引用发现的奇怪问题

1:编辑器内容加粗样式不好使,但是获取到的内容有strong标签;

解决方法:

/deep/strong {
  font-weight: bold !important;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值