Vue2.0中整合UEditor富文字編輯器的方法,vue2.0ueditor

來源:互聯網
上載者:User

Vue2.0中整合UEditor富文字編輯器的方法,vue2.0ueditor

在vue的'項目中遇到了需要使用富文字編輯器的需求,在github上看了很多vue封裝的editor外掛程式,很多對圖片上傳和視頻上傳的支援並不是很好,最終還是決定使用UEditor。

這類的文章網上有很多,我進行了摸索、手寫代碼、匯總、排版,形成了這篇文章。

下載對應的UEditor源碼

首先,去官網上下載UEditor的源碼,根據你後台語言的不同下載對應的版本(PHP、Asp、.Net、Jsp)。

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

下載之後,把資源放到 /static/ue/ 靜態目錄下。文檔結構如下:

(我把UEditor放到了static靜態目錄下面,這裡的檔案不會被webpack打包,當然你也可以選擇性地放進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檔案有很多配置,可以在這裡進行一些初始化的全域配置,比如編輯器的預設寬高等:
,initialFrameWidth:1000 //初始化編輯器寬度,預設1000,initialFrameHeight:320 //初始化編輯器高度,預設320
其他的參數配置,在該檔案中有詳細列出,或者參考官方文檔 http://fex.baidu.com/ueditor/

將編輯器整合到系統中

開啟 /src/main.js 檔案,插入下面的代碼:

//ueditorimport '../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'

開發公用組件 UE.vue

我們在 /src/components/ 目錄下建立 UE.vue檔案,作為我們的編輯器組件檔案。

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

<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>

組件暴露了兩個介面:

  • value是編輯器的文字
  • config是編輯器的配置參數

在其他頁面中使用該組件

簡單地建立一個需要UEditor的頁面,再該頁面中使用剛才封裝好的UE.vue組件:

<template>  <div>    <Uediter :value="ueditor.value" :config="ueditor.config" ref="ue"></Uediter>    <button @click="returnContent">顯示編輯器內容</el-button>    <div>{{dat.content}}</div>  </div></template><script>  import Uediter from '@/components/UE.vue';  export default {    data () {      return {        dat: {          content: ''        },        ueditor: {          value: '編輯器預設文字',          config: {            initialFrameWidth: 800,            initialFrameHeight: 320          }        }      }    },    methods: {      returnContent () {        this.dat.content = this.$refs.ue.getUEContent()      }    },    components: {      Uediter    },  }</script>

效果如下:

What's more: 服務端需要做的配置

配置完上述內容後,控制台可能會出現"後台配置項返回格式出錯,上傳功能將不能正常使用!"的報錯,
我們在編輯器中上傳圖片或者視頻,也會出現響應的報錯,這是因為沒有設定管理員的請求介面,在ueditor.config.js中,對serverUrl進行配置:

// 伺服器統一請求介面路徑, serverUrl: 'http://172.16.254.49:83/File/UEditor'  //地址管你們後端要去

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.