vue項目中使用wangEditor富文本

1,vue中安裝wangEditor
使用的npm安裝 npm install wangeditor --save
2,創(chuàng)建公用組件
在components中創(chuàng)建wangEnduit文件夾
組件內(nèi)容為

<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) {
        // 觸發(fā)清除文本域內(nèi)容
        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為編輯框輸入的內(nèi)容,這里我監(jiān)聽了一下值,當(dāng)父組件調(diào)用得時候,如果給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 = 'http://otp.cdinfotech.top/file/upload_images'// 配置服務(wù)器端地址
        this.editor.customConfig.uploadImgHeaders = { }// 自定義 header
        this.editor.customConfig.uploadFileName = 'file' // 后端接受上傳文件的參數(shù)名
        this.editor.customConfig.uploadImgMaxSize = 2 * 1024 * 1024 // 將圖片大小限制為 2M
        this.editor.customConfig.uploadImgMaxLength = 6 // 限制一次最多上傳 3 張圖片
        this.editor.customConfig.uploadImgTimeout = 3 * 60 * 1000 // 設(shè)置超時時間

        // 配置菜單
        this.editor.customConfig.menus = [
          'head', // 標(biāo)題
          'bold', // 粗體
          'fontSize', // 字號
          'fontName', // 字體
          'italic', // 斜體
          'underline', // 下劃線
          'strikeThrough', // 刪除線
          'foreColor', // 文字顏色
          'backColor', // 背景顏色
          'link', // 插入鏈接
          'list', // 列表
          'justify', // 對齊方式
          'quote', // 引用
          'emoticon', // 表情
          'image', // 插入圖片
          'table', // 表格
          'video', // 插入視頻
          'code', // 插入代碼
          'undo', // 撤銷
          'redo', // 重復(fù)
          'fullscreen' // 全屏
        ]

        this.editor.customConfig.uploadImgHooks = {
          fail: (xhr, editor, result) => {
            // 插入圖片失敗回調(diào)
          },
          success: (xhr, editor, result) => {
            // 圖片上傳成功回調(diào)
          },
          timeout: (xhr, editor) => {
            // 網(wǎng)絡(luò)超時的回調(diào)
          },
          error: (xhr, editor) => {
            // 圖片上傳錯誤的回調(diào)
          },
          customInsert: (insertImg, result, editor) => {
            // 圖片上傳成功,插入圖片的回調(diào)
            //result為上傳圖片成功的時候返回的數(shù)據(jù),這里我打印了一下發(fā)現(xiàn)后臺返回的是data:[{url:"路徑的形式"},...]
            // console.log(result.data[0].url)
            //insertImg()為插入圖片的函數(shù)
             //循環(huán)插入圖片
            // 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 // 綁定當(dāng)前逐漸地值
          this.$emit('change', this.info_) // 將內(nèi)容同步到父組件中
        }
        // 創(chuàng)建富文本編輯器
        this.editor.create()
      }
    }
  }
</script>

<style lang="css">
  .editor {
    width: 100%;
    margin: 0 auto;
    position: relative;
    z-index: 0;
  }
  .toolbar {
    border: 1px solid #ccc;
  }
  .text {
    border: 1px solid #ccc;
    min-height: 500px;
  }
</style>

3 在父組件中調(diào)用

<template>
<div>
<editor-bar v-model="detail" :isClear="isClear" @change="change"></editor-bar>
</div>
</template>

import EditorBar from './editoritem'
components: { EditorBar },
data() {
      return {
        isClear: false,
        detail:""
        }
      },  
methods: {
  change(val) {
      console.log(val)
      
    },
 }

wangEditor官方文檔
轉(zhuǎn)載至 --> 冷影再見博客 https://kongjz.com/wly/detail/91

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容