需求:一个页面需要多个 富文本编辑器  ,暂选vue-quill-editor。

在使用过程中,添加  上传图片、视频的操作。单个编辑器正常使用,复用之后,第二个编辑器上传成功后找不到quill正确对象,还是用的第一个编辑器的对象。(请教大神后,需添加index,如下图,需要传不同的组件id,当时着急就没有仔细调试了),还未使用,目前使用vue-quill-editor  和 vue-quill-editor-extend,哈哈哈哈,当时急着用,增强版的里面没有视频,还得单独写,就两个一起用了。

更新同一组件冲突问题:简单解决方法,实例化两次,分别调用。亲测管用。我在试试传不同组件id试试

<el-form-item label="绘本介绍">
          <UploadEditorImage1 :pcontent="form.introduction" @content="childByIntroduction" key="1"></UploadEditorImage1>
        </el-form-item>
        <el-form-item label="其他">
          <!-- <el-input type="textarea" :rows="2" placeholder="请输入内容" v-model="form.content"></el-input> -->
          <UploadEditorImage :pcontent="form.content" @content="childByContent" key="2"></UploadEditorImage>
        </el-form-item>
...
...
...

import UploadEditorImage from "../util/UploadEditorImage";
import UploadEditorImage1 from "../util/UploadEditorImage";
export default {
  components: { /*UploadEditor,*/ UploadEditorImage,UploadEditorImage1 }, //新增专辑、音频页面

下载vue-quill-editor ,vue-quill-editor-extend

Editor.vue 

<template>
  <div>
    <!-- 图片上传组件辅助-->
    <el-upload
      class="pic-uploader"
      :action="serverUrl"
      accept="image/jpg, image/jpeg, image/png"
      :data="{method:'upload',type:'image'}"
      :show-file-list="false"
      :on-success="uploadSuccess"
      :on-error="uploadError"
      :before-upload="beforeUpload"
    ></el-upload>
    <!-- 视频上传组件辅助-->
    <el-upload
      class="mp4-uploader"
      :action="serverUrl"
      accept="audio/mpeg"
      :data="{method:'upload',type:'video'}"
      :show-file-list="false"
      :on-success="uploadSuccess"
      :on-error="uploadError"
      :before-upload="beforeUpload"
    ></el-upload>
    <quill-editor
      class="editor"
      v-model="content"
      :ref="myQuillEditor"
      :options="editorOption"
      @blur="onEditorBlur($event)"
      @focus="onEditorFocus($event)"
      @change="onEditorChange($event)"
    ></quill-editor>
  </div>
</template>

<script>
import { addQuillTitle } from "./quill-title.js";

// 工具栏配置
const toolbarOptions = [
  ["bold", "italic", "underline", "strike"], // 加粗 斜体 下划线 删除线
  ["blockquote", "code-block"], // 引用  代码块
  [{ list: "ordered" }, { list: "bullet" }], // 有序、无序列表
  [{ indent: "-1" }, { indent: "+1" }], // 缩进
  [{ size: ["small", false, "large", "huge"] }], // 字体大小
  [{ header: [1, 2, 3, 4, 5, 6, false] }], // 标题
  [{ color: [] }, { background: [] }], // 字体颜色、字体背景颜色
  [{ font: [] }], // 字体种类
  [{ align: [] }], // 对齐方式
  ["clean"], // 清除文本格式
  ["link", "image", "video"] // 链接、图片、视频
];
export default {
  props: {
    pcontent: ""
  },
  data() {
    return {
      myQuillEditor: "myQuillEditor",
      content: "",
      quillUpdateImg: false, // 根据图片上传状态来确定是否显示loading动画,刚开始是false,不显示
      editorOption: {
        placeholder: "",
        theme: "snow", // or 'bubble'
        placeholder: "",
        modules: {
          toolbar: {
            container: toolbarOptions,
            handlers: {
              image: function(value) {
                if (value) {
                  // 触发input框选择图片文件
                  document.querySelector(".pic-uploader input").click();
                } else {
                  this.quill.format("image", false);
                }
              },
              video: function(value) {
                if (value) {
                  // 触发input框选择视频文件
                  document.querySelector(".mp4-uploader input").click();
                } else {
                  this.quill.format("video", false);
                }
              }
            }
          }
        }
      },
      serverUrl: "http:/****k.php", // 这里写你要上传的图片服务器地址
      header: {
        // token: sessionStorage.token   有的图片服务器要求请求头需要有token
      }
    };
  },
  created: function() {
    var that = this;
    that.content = that.pcontent;
  },
  watch: {
    pcontent(newValue, oldValue) {
      var that = this;
      that.content = newValue;
    }
  },
  mounted() {
    addQuillTitle();
  },
  methods: {
    onEditorBlur() {
      //失去焦点事件
    },
    onEditorFocus() {
      //获得焦点事件
    },
    onEditorChange() {
      //内容改变事件
      this.$emit("content", this.content);
    },
    // 富文本图片、视频上传前
    beforeUpload() {
      // 显示loading动画
      this.quillUpdateImg = true;
    },
    uploadSuccess(res, file) {
      console.log(this.id);
      let quill = this.$refs.myQuillEditor.quill; // 获取富文本组件实例
      if (res.code == 1) {
        let length = quill.getSelection().index; // 获取光标所在位置
        quill.insertEmbed(length, res.data.type, res.data.url); // 插入资源  res.url为服务器返回的地址
        quill.setSelection(length + 1); // 调整光标到最后
      } else {
        this.$message.error("插入失败");
      }
      // loading动画消失
      this.quillUpdateImg = false;
    },
    // 富文本图片上传失败
    uploadError() {
      // loading动画消失
      this.quillUpdateImg = false;
      this.$message.error("资源插入失败");
    }
  }
};
</script> 

<style>
.pic-uploader {
  display: none;
}
.mp4-uploader {
  display: none;
}
.editor {
  line-height: normal !important;
  height: 150px;
  padding-bottom: 20px;
  margin-bottom: 20px;
}
</style>

增强版的

<template>
  <div class="quill-wrap">
    <quill-editor
      class="editor"
      v-model="content"
      ref="myQuillEditor"
      :options="editorOption"
      @blur="onEditorBlur($event)"
      @focus="onEditorFocus($event)"
      @change="onEditorChange($event)"
    ></quill-editor>
  </div>
</template>
<script>
import { quillEditor, Quill } from "vue-quill-editor";
import { container, ImageExtend, QuillWatch } from "quill-image-extend-module";
Quill.register("modules/ImageExtend", ImageExtend);

import { addQuillTitle } from "./quill-title.js";
// 工具栏配置
const toolbarOptions = [
  ["bold", "italic", "underline", "strike"], // 加粗 斜体 下划线 删除线
  ["blockquote", "code-block"], // 引用  代码块
  [{ list: "ordered" }, { list: "bullet" }], // 有序、无序列表
  [{ indent: "-1" }, { indent: "+1" }], // 缩进
  [{ size: ["small", false, "large", "huge"] }], // 字体大小
  [{ header: [1, 2, 3, 4, 5, 6, false] }], // 标题
  [{ color: [] }, { background: [] }], // 字体颜色、字体背景颜色
  [{ font: [] }], // 字体种类
  [{ align: [] }], // 对齐方式
  ["clean"], // 清除文本格式
  ["link", "image"] // 链接、图片
];

export default {
  props: {
    pcontent: ""
  },
  components: { quillEditor },
  data() {
    return {
      content: "",
      // 富文本框参数设置
      editorOption: {
        placeholder: "",
        modules: {
          ImageExtend: {
            loading: true,
            name: "file",
            action:
              "http://baby.ci123.c******.php?method=upload&type=image",
            response: res => {
              console.log(res);
              return res.data.url;
            }
          },
          toolbar: {
            container: toolbarOptions,
            handlers: {
              image: function() {
                QuillWatch.emit(this.quill.id);
              }
            }
          }
        }
      }
    };
  },
  mounted() {
    addQuillTitle();
  },
  created: function() {
    var that = this;
    that.content = that.pcontent;
  },
  watch: {
    pcontent(newValue, oldValue) {
      var that = this;
      that.content = newValue;
    }
  },
  methods: {
    onEditorBlur() {
      //失去焦点事件
    },
    onEditorFocus() {
      //获得焦点事件
    },
    onEditorChange() {
      //内容改变事件
      this.$emit("content", this.content);
    }
  }
};
</script>
<style>
.pic-uploader {
  display: none;
}
.mp4-uploader {
  display: none;
}
.editor {
  line-height: normal !important;
  height: 150px;
  padding-bottom: 20px;
  margin-bottom: 20px;
}
</style>

 

Logo

前往低代码交流专区

更多推荐