1. 程式人生 > 實用技巧 >Vue中使用wangEditor富文字編輯器

Vue中使用wangEditor富文字編輯器

1,vue中安裝wangEditor

使用的npm安裝 npm install wangeditor --save

2,建立公用元件

在components中建立wangEnduit資料夾

元件內容為:

<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 = 'http://otp.cdinfotech.top/file/upload_images'// 配置伺服器端地址 this.editor.customConfig.uploadImgHeaders = { }// 自定義 header this.editor.customConfig.uploadFileName = 'file' // 後端接受上傳檔案的引數名 this.editor.customConfig.uploadImgMaxSize = 2 * 1024 * 1024 // 將圖片大小限制為 2M this.editor.customConfig.uploadImgMaxLength = 6 // 限制一次最多上傳 3 張圖片 this.editor.customConfig.uploadImgTimeout = 3 * 60 * 1000 // 設定超時時間 // 配置選單 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.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、在父元件中呼叫

<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)
      
    },
 }