前言
創(chuàng)新互聯(lián)建站專注于肅州網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供肅州營銷型網(wǎng)站建設(shè),肅州網(wǎng)站制作、肅州網(wǎng)頁設(shè)計、肅州網(wǎng)站官網(wǎng)定制、重慶小程序開發(fā)公司服務(wù),打造肅州網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供肅州網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
前一段時間公司Vue.js項目需要使用UEditor富文本編輯器,在百度上搜索一圈沒有發(fā)現(xiàn)詳細的說明,決定自己嘗試,忙活了一天終于搞定了。
具體可以參考這篇文章:https://www.jb51.net/article/118413.htm
ueditor是百度編輯器,官網(wǎng)地址:http://ueditor.baidu.com/website/
完整的功能演示,可以參考:http://ueditor.baidu.com/website/onlinedemo.html
最近工作中要求升級,需要在vue中創(chuàng)建多個ueditor實例,我使用neditor,其實就是把ueditor樣式美化了下,其他和ueditor幾乎一樣,下面話不多說了,來一起看看詳細的介紹吧。
截圖
說明
下載ueditor或neditor源碼,拷貝到static目錄下面
然后修改ueditor.config.js配置文件
在vue項目的main.js添加ueditor引用
新建3個頁面 home,tab1,tab2。tab1和tab2是home下面的子頁面
在router-view外面一定要添加keep-alive組件和transition組件,不然ueditor實例無法保存
在components文件夾下面新建一個editor作為編輯器的公共組件
在tab1中調(diào)用editor,同時要傳入一個id并在editor頁面接受,注意如果需要多個實例,id一定不能相同
<template> <div> <editor ref="editor" id="tab1Editor"></editor> <button @click="getContent" class="m-t-10">獲取內(nèi)容</button> <div> <span>當前富文本編輯器內(nèi)容是: {{content}}</span> </div> </div> </template> <script> import Editor from '@/components/editor' export default { name: 'tab1', components: { Editor }, data() { return { content:'' } }, methods: { //獲取內(nèi)容 getContent(){ this.content = this.$refs.editor.content } } } </script> <style scoped> .m-t-10{ margin-top: 10px; } </style>
editor頁面代碼,因為我們在router-view套用了keep-alive,所以ueditor的初始化一定要放在activated里面,
確保每次進入頁面都會重新渲染ueditor,在deactivated里面調(diào)用ueditor的destroy方法,確保每次離開頁面的時候
會銷毀編輯器實例,這樣就可以渲染多個ueditor實例了,并且每次切換都能保存編輯器的內(nèi)容。
如果多個tab只需要一個實例請調(diào)用reset()方法
<template> <div> <div :id="this.id"></div> </div> </template> <script> export default { name: 'editor', props: ['id'], data() { return { ue: '', //ueditor實例 content: '', //編輯器內(nèi)容 } }, methods: { //初始化編輯器 initEditor() { this.ue = UE.getEditor(this.id, { initialFrameWidth: '100%', initialFrameHeight: '350', scaleEnabled: true }) //編輯器準備就緒后會觸發(fā)該事件 this.ue.addListener('ready',()=>{ //設(shè)置可以編輯 this.ue.setEnabled() }) //編輯器內(nèi)容修改時 this.selectionchange() }, //編輯器內(nèi)容修改時 selectionchange() { this.ue.addListener('selectionchange', () => { this.content = this.ue.getContent() }) } }, activated() { //初始化編輯器 this.initEditor() }, deactivated() { //銷毀編輯器實例,使用textarea代替 this.ue.destroy() //重置編輯器,可用來做多個tab使用同一個編輯器實例 //如果要使用同一個實例,請注釋destroy()方法 //this.ue.reset() } } </script>
源碼地址
github:https://github.com/oblivioussing/vue-ueditor-multi
本地下載:http://xiazai.jb51.net/201711/yuanma/vue-ueditor-multi(jb51.net).rar
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對創(chuàng)新互聯(lián)的支持。
文章名稱:vue中如何創(chuàng)建多個ueditor實例教程
網(wǎng)站網(wǎng)址:http://bm7419.com/article12/pcdjdc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、手機網(wǎng)站建設(shè)、網(wǎng)站設(shè)計公司、商城網(wǎng)站、標簽優(yōu)化、網(wǎng)站維護
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)