溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Vue2.0中如何集成UEditor富文本編輯器

發(fā)布時(shí)間:2021-08-13 09:18:41 來源:億速云 閱讀:152 作者:小新 欄目:web開發(fā)

小編給大家分享一下Vue2.0中如何集成UEditor富文本編輯器,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

首先,去官網(wǎng)上下載UEditor的源碼,根據(jù)你后臺(tái)語言的不同下載對(duì)應(yīng)的版本(PHP、Asp、.Net、Jsp)。

http://ueditor.baidu.com/website/download.html

下載之后,把資源放到 /static/ue/ 靜態(tài)目錄下。文檔結(jié)構(gòu)如下:

Vue2.0中如何集成UEditor富文本編輯器

(我把UEditor放到了static靜態(tài)目錄下面,這里的文件不會(huì)被webpack打包,當(dāng)然你也可以選擇性地放進(jìn)src中)

編輯 UEditor 編輯器 配置文件

我們打開 ueditor.config.js,修改其中的window.UEDITOR_HOME_UR配置,如下:

window.UEDITOR_HOME_URL = "/static/UE/";   //指定編輯器資源文件根目錄
var URL = window.UEDITOR_HOME_URL || getUEBasePath();

ueditor.config.js文件有很多配置,可以在這里進(jìn)行一些初始化的全局配置,比如編輯器的默認(rèn)寬高等:

,initialFrameWidth:1000 //初始化編輯器寬度,默認(rèn)1000
,initialFrameHeight:320 //初始化編輯器高度,默認(rèn)320

其他的參數(shù)配置,在該文件中有詳細(xì)列出,或者參考官方文檔 http://fex.baidu.com/ueditor/

將編輯器集成到系統(tǒng)中

打開 /src/main.js 文件,插入下面的代碼:

//ueditor
import '../static/UE/ueditor.config.js'
import '../static/UE/ueditor.all.min.js'
import '../static/UE/lang/zh-cn/zh-cn.js'
import '../static/UE/ueditor.parse.min.js'

開發(fā)公共組件 UE.vue

我們在 /src/components/ 目錄下創(chuàng)建 UE.vue文件,作為我們的編輯器組件文件。

下面代碼提供簡單功能,具體使用根據(jù)需求完善該組件即可。

<template>
  <div>
    <script type="text/plain"></script>
  </div>
</template>
<script>
  export default {
    name: 'ue',
    data () {
      return {
        editor: null
      }
    },
    props: {
      value: '',
      config: {}
    },
    mounted () {
      this.editor = window.UE.getEditor('editor', this.config);
      this.editor.addListener('ready', () => {
        this.editor.setContent(this.value)
      })
    },
    methods: {
      getUEContent () {
        return this.editor.getContent()
      }
    },
    destroyed () {
      this.editor.destroy()
    }
  }
</script>

組件暴露了兩個(gè)接口:

  • value是編輯器的文字

  • config是編輯器的配置參數(shù)

在其他頁面中使用該組件

簡單地創(chuàng)建一個(gè)需要UEditor的頁面,再該頁面中使用剛才封裝好的UE.vue組件:

<template>
  <div>
    <Uediter :value="ueditor.value" :config="ueditor.config" ref="ue"></Uediter>
    <button @click="returnContent">顯示編輯器內(nèi)容</el-button>
    <div>{{dat.content}}</div>
  </div>
</template>
<script>
  import Uediter from '@/components/UE.vue';

  export default {
    data () {
      return {
        dat: {
          content: ''
        },
        ueditor: {
          value: '編輯器默認(rèn)文字',
          config: {
            initialFrameWidth: 800,
            initialFrameHeight: 320
          }
        }
      }
    },
    methods: {
      returnContent () {
        this.dat.content = this.$refs.ue.getUEContent()
      }
    },
    components: {
      Uediter
    },
  }
</script>

效果如下:

Vue2.0中如何集成UEditor富文本編輯器

What's more: 服務(wù)端需要做的配置

配置完上述內(nèi)容后,控制臺(tái)可能會(huì)出現(xiàn)"后臺(tái)配置項(xiàng)返回格式出錯(cuò),上傳功能將不能正常使用!"的報(bào)錯(cuò),
我們在編輯器中上傳圖片或者視頻,也會(huì)出現(xiàn)響應(yīng)的報(bào)錯(cuò),這是因?yàn)闆]有配置服務(wù)器的請(qǐng)求接口,在ueditor.config.js中,對(duì)serverUrl進(jìn)行配置:

// 服務(wù)器統(tǒng)一請(qǐng)求接口路徑
, serverUrl: 'http://172.16.254.49:83/File/UEditor'  //地址管你們后端要去

以上是“Vue2.0中如何集成UEditor富文本編輯器”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI