天天看點

[vue] 使用wangeditor富文本編輯器

  1. 第一步:下載下傳安裝wangeditor,我要說的是,别安裝成2.0版本了,裝3.0版本,

    安裝完成後在package.json檔案中可以看到圖2

    [vue] 使用wangeditor富文本編輯器
    [vue] 使用wangeditor富文本編輯器
  2. 在components檔案下新增一個檔案,命名editor.vue(無所謂,隻是後面引入時要注意)
    [vue] 使用wangeditor富文本編輯器
  3. 在editor.vue檔案中寫下面代碼` (子元件)
<template lang="html">
  <!-- 富文本編輯器 -->
  <div class="editor">
    <div ref="toolbar" class="toolbar">
    </div>
    <div ref="editor" class="text">
    </div>
  </div>
</template>

<script>
import E from "wangeditor";
export default {
  name: "editoritem",
  data() {
    return {
      // uploadPath,
      editor: null,
      info_: null
    };
  },
  model: {
    prop: "value",
    event: "change"
  },
  props: {
    value: {
      type: String,
      default: ""
    },
    isClear: {
      type: Boolean,
      default: false
    }
  },
  watch: {
    isClear(val) {
      // 觸發清除文本域内容
      if (val) {
        this.editor.txt.clear();
        this.info_ = null;
      }
    },
    // value: function(value) {
    //   if (value !== this.editor.txt.html()) {
    //     this.editor.txt.html(this.value);
    //   }
    // }
    //value為編輯框輸入的内容,這裡我監聽了一下值,當父元件調用得時候,如果給value指派了,子元件将會顯示父元件賦給的值
  },
  mounted() {
    this.seteditor();
    this.editor.txt.html(this.value);
  },
  methods: {
    seteditor() {
      // http://192.168.2.125:8080/admin/storage/create
      this.editor = new E(this.$refs.toolbar, this.$refs.editor);
      this.editor.customConfig.uploadImgShowBase64 = false; // base 64 存儲圖檔
      this.editor.customConfig.uploadImgServer = ""; // 配置伺服器端位址
      this.editor.customConfig.uploadImgHeaders = {}; // 自定義 header
      this.editor.customConfig.uploadFileName = " "; // 後端接受上傳檔案的參數名
      this.editor.customConfig.uploadImgMaxSize = 2 * 1024 * 1024; // 将圖檔大小限制為 2M
      this.editor.customConfig.uploadImgMaxLength = 6; // 限制一次最多上傳 3 張圖檔
      this.editor.customConfig.uploadImgTimeout = 3 * 60 * 1000; // 設定逾時時間

      // 配置菜單  (如果安裝的是2.0版本的wangeditor,在配置菜單的時候有些功能可能就沒有)
      this.editor.customConfig.menus = [
        "head", // 标題
        "bold", // 粗體
        "fontSize", // 字号
        "fontName", // 字型
        "italic", // 斜體
        "underline", // 下劃線
        "strikeThrough", // 删除線
        "foreColor", // 文字顔色
        "backColor", // 背景顔色
        "link", // 插傳入連結接
        "list", // 清單
        "justify", // 對齊方式
        "quote", // 引用
        "emoticon", // 表情
        "image", // 插入圖檔
        "table", // 表格
        "video", // 插入視訊
        "code", // 插入代碼
        "undo", // 撤銷
        "redo", // 重複
        // "fullscreen" // 全屏
      ];
      this.editor.customConfig.uploadImgHooks = {
        fail: (xhr, editor, result) => {
          // 插入圖檔失敗回調
        },
        success: (xhr, editor, result) => {
          // 圖檔上傳成功回調
        },
        timeout: (xhr, editor) => {
          // 網絡逾時的回調
        },
        error: (xhr, editor) => {
          // 圖檔上傳錯誤的回調
        },
        customInsert: (insertImg, result, editor) => {
          // 圖檔上傳成功,插入圖檔的回調
          //result為上傳圖檔成功的時候傳回的資料,這裡我列印了一下發現背景傳回的是data:[{url:"路徑的形式"},...]
          // console.log(result.data[0].url)
          //insertImg()為插入圖檔的函數
          //循環插入圖檔
          // for (let i = 0; i < 1; i++) {
          // console.log(result)
          let url = "http://otp.cdinfotech.top" + result.url;
          insertImg(url);
          // }
        }
      };
      this.editor.customConfig.onchange = html => {
        this.info_ = html; // 綁定目前逐漸地值
        this.$emit("change", this.info_); // 将内容同步到父元件中
      };

      // this.editor.customConfig.onchangeTimeout = 99999999999999999999999999999
      // 建立富文本編輯器
      this.editor.create();
      
    },
  }
};
</script>

<style lang="css">
           
  1. 父元件(命名随便)
<template>
  <div>
    <editor-bar v-model="detail" :isClear="isClear" @change="change"></editor-bar>
    <el-button @click="input">輸出</el-button>
  </div>
</template>

<script>
import EditorBar from "./editor";
export default {
  components: {
    EditorBar
  },
  data() {
    return {
      isClear: false,
      detail:''
    };
  },
  methods: {
    change(val) {
    // 重要: 要想在編輯器裡的内容等輸入完成後才觸發,就可以把子元件中watch下的value注釋掉,讓使用者手動觸發
       this.detail = val
    },
    input(){
      //這個輸出按鈕隻是我用來得到編輯器裡的内容
      console.log(this.detail); //可以得到編輯器裡的最新更新内容
      
    }
  },
  created() {
    //這個是我想一進入頁面就有内容才加的,直接指派就行
    this.detail = ``
  },
  
};
</script>

<style>
</style>
           
  1. 下面是效果
    [vue] 使用wangeditor富文本編輯器