vue2.0項(xiàng)目中使用Ueditor富文本編輯器示例代碼
最近在vue項(xiàng)目中需要使用富文本編輯器,于是將Ueditor集成進(jìn)來,作為公共組件。
項(xiàng)目地址:https://github.com/suweiteng/vue2-management-platform
1.放入靜態(tài)資源并配置
首先把官網(wǎng)下載的Ueditor資源,放入靜態(tài)資源src/static中。
修改ueditor.config.js中的window.UEDITOR_HOME_URL配置,如下圖:
2.引入
在main.js中引入
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'
3.開發(fā)公共組件
開發(fā)公共組件,可設(shè)置填充內(nèi)容defaultMsg,配置信息config(寬度和高度等),并提供獲取內(nèi)容的方法。
<template> <div> <script id="editor" type="text/plain"></script> </div> </template> <script> export default { name: 'UE', data () { return { editor: null } }, props: { defaultMsg: { type: String }, config: { type: Object } }, mounted() { const _this = this; this.editor = UE.getEditor('editor', this.config); // 初始化UE this.editor.addListener("ready", function () { _this.editor.setContent(_this.defaultMsg); // 確保UE加載完成后,放入內(nèi)容。 }); }, methods: { getUEContent() { // 獲取內(nèi)容方法 return this.editor.getContent() } }, destroyed() { this.editor.destroy(); } } </script>
4.使用
當(dāng)我們需要使用富文本編輯器時,直接調(diào)用公共組件即可
<template> <div class="components-container"> <div class="info">UE編輯器示例<br>需要使用編輯器時,調(diào)用UE公共組件即可??稍O(shè)置填充內(nèi)容defaultMsg,配置信息config(寬度和高度等),可調(diào)用組件中獲取內(nèi)容的方法。</div> <button @click="getUEContent()">獲取內(nèi)容</button> <div class="editor-container"> <UE :defaultMsg=defaultMsg :config=config ref="ue"></UE> </div> </div> </template> <style> .info{ border-radius: 10px; line-height: 20px; padding: 10px; margin: 10px; background-color: #ffffff; } </style> <script> import UE from '../../components/ue/ue.vue'; export default { components: {UE}, data() { return { defaultMsg: '這里是UE測試', config: { initialFrameWidth: null, initialFrameHeight: 350 } } }, methods: { getUEContent() { let content = this.$refs.ue.getUEContent(); this.$notify({ title: '獲取成功,可在控制臺查看!', message: content, type: 'success' }); console.log(content) } } }; </script>
效果如下:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Element基于el-input數(shù)字范圍輸入框的實(shí)現(xiàn)
本文主要介紹了?Element基于el-input數(shù)字范圍輸入框的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04vue + any-touch實(shí)現(xiàn)一個iscroll 實(shí)現(xiàn)拖拽和滑動動畫效果
這篇文章主要介紹了vue + any-touch實(shí)現(xiàn)一個iscroll實(shí)現(xiàn)拖拽和滑動動畫效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-04-04vue video和vue-video-player實(shí)現(xiàn)視頻鋪滿教程
這篇文章主要介紹了vue video和vue-video-player實(shí)現(xiàn)視頻鋪滿教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10詳解新手使用vue-router傳參時注意事項(xiàng)
這篇文章主要介紹了詳解新手使用vue-router傳參時注意事項(xiàng),詳細(xì)的介紹了幾種常見錯誤,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06vue elementUI使用tabs與導(dǎo)航欄聯(lián)動
這篇文章主要為大家詳細(xì)介紹了vue elementUI使用tabs與導(dǎo)航欄聯(lián)動,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-06-06