vue中如何創(chuàng)建多個(gè)ueditor實(shí)例教程
前言
前一段時(shí)間公司Vue.js項(xiàng)目需要使用UEditor富文本編輯器,在百度上搜索一圈沒(méi)有發(fā)現(xiàn)詳細(xì)的說(shuō)明,決定自己嘗試,忙活了一天終于搞定了。
具體可以參考這篇文章:http://chabaoo.cn/article/118413.htm
ueditor是百度編輯器,官網(wǎng)地址:http://ueditor.baidu.com/website/
完整的功能演示,可以參考:http://ueditor.baidu.com/website/onlinedemo.html
最近工作中要求升級(jí),需要在vue中創(chuàng)建多個(gè)ueditor實(shí)例,我使用neditor,其實(shí)就是把ueditor樣式美化了下,其他和ueditor幾乎一樣,下面話不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧。
截圖
說(shuō)明
下載ueditor或neditor源碼,拷貝到static目錄下面
然后修改ueditor.config.js配置文件
在vue項(xiàng)目的main.js添加ueditor引用
新建3個(gè)頁(yè)面 home,tab1,tab2。tab1和tab2是home下面的子頁(yè)面
在router-view外面一定要添加keep-alive組件和transition組件,不然ueditor實(shí)例無(wú)法保存
在components文件夾下面新建一個(gè)editor作為編輯器的公共組件
在tab1中調(diào)用editor,同時(shí)要傳入一個(gè)id并在editor頁(yè)面接受,注意如果需要多個(gè)實(shí)例,id一定不能相同
<template> <div> <editor ref="editor" id="tab1Editor"></editor> <button @click="getContent" class="m-t-10">獲取內(nèi)容</button> <div> <span>當(dāng)前富文本編輯器內(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頁(yè)面代碼,因?yàn)槲覀冊(cè)趓outer-view套用了keep-alive,所以u(píng)editor的初始化一定要放在activated里面,
確保每次進(jìn)入頁(yè)面都會(huì)重新渲染ueditor,在deactivated里面調(diào)用ueditor的destroy方法,確保每次離開頁(yè)面的時(shí)候
會(huì)銷毀編輯器實(shí)例,這樣就可以渲染多個(gè)ueditor實(shí)例了,并且每次切換都能保存編輯器的內(nèi)容。
如果多個(gè)tab只需要一個(gè)實(shí)例請(qǐng)調(diào)用reset()方法
<template> <div> <div :id="this.id"></div> </div> </template> <script> export default { name: 'editor', props: ['id'], data() { return { ue: '', //ueditor實(shí)例 content: '', //編輯器內(nèi)容 } }, methods: { //初始化編輯器 initEditor() { this.ue = UE.getEditor(this.id, { initialFrameWidth: '100%', initialFrameHeight: '350', scaleEnabled: true }) //編輯器準(zhǔn)備就緒后會(huì)觸發(fā)該事件 this.ue.addListener('ready',()=>{ //設(shè)置可以編輯 this.ue.setEnabled() }) //編輯器內(nèi)容修改時(shí) this.selectionchange() }, //編輯器內(nèi)容修改時(shí) selectionchange() { this.ue.addListener('selectionchange', () => { this.content = this.ue.getContent() }) } }, activated() { //初始化編輯器 this.initEditor() }, deactivated() { //銷毀編輯器實(shí)例,使用textarea代替 this.ue.destroy() //重置編輯器,可用來(lái)做多個(gè)tab使用同一個(gè)編輯器實(shí)例 //如果要使用同一個(gè)實(shí)例,請(qǐng)注釋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é)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
關(guān)于keep-alive路由多級(jí)嵌套不生效的解決方案
本文主要介紹了關(guān)于keep-alive路由多級(jí)嵌套不生效的解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03vue如何動(dòng)態(tài)設(shè)置背景漸變色
這篇文章主要介紹了vue如何動(dòng)態(tài)設(shè)置背景漸變色問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08vue項(xiàng)目下載文件重命名監(jiān)測(cè)進(jìn)度demo
這篇文章主要為大家介紹了vue項(xiàng)目下載文件重命名監(jiān)測(cè)進(jìn)度demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10vue router返回到指定的路由的場(chǎng)景分析
這篇文章主要介紹了vue router返回到指定的路由的場(chǎng)景分析,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11Vue+Bootstrap實(shí)現(xiàn)簡(jiǎn)易學(xué)生管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Vue+Bootstrap實(shí)現(xiàn)簡(jiǎn)易學(xué)生管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-02-02Vue使用el-table實(shí)現(xiàn)表格跨頁(yè)多選
在我們?nèi)粘m?xiàng)目開發(fā)中,經(jīng)常會(huì)有表格跨頁(yè)多選的需求,接下來(lái)讓我們用?el-table示例一步步來(lái)實(shí)現(xiàn)這個(gè)需求,文中有詳細(xì)的代碼講解,對(duì)我們的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-08-08