vue 使用vue-quill-editor 富文本編輯器 demo 的問題,以及一個頁面多個富文本編輯器出現的衝突。

 

需求:一個頁面需要多個 富文本編輯器  ,暫選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>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章