源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

vue中如何创建多个ueditor实例教程

  • 时间:2022-08-18 11:46 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:vue中如何创建多个ueditor实例教程
[b]前言[/b] 前一段时间公司Vue.js项目需要使用UEditor富文本编辑器,在百度上搜索一圈没有发现详细的说明,决定自己尝试,忙活了一天终于搞定了。 [b]具体可以参考这篇文章:[/b][url=http://www.1sucai.cn/article/118413.htm]http://www.1sucai.cn/article/118413.htm[/url] ueditor是百度编辑器,官网地址:http://ueditor.baidu.com/website/ 完整的功能演示,可以参考:http://ueditor.baidu.com/website/onlinedemo.html 最近工作中要求升级,需要在vue中创建多个ueditor实例,我使用neditor,其实就是把ueditor样式美化了下,其他和ueditor几乎一样,下面话不多说了,来一起看看详细的介绍吧。 [b]截图[/b] [img]http://files.jb51.net/file_images/article/201711/2017111493802792.gif?2017101493814[/img] [b]说明[/b] 下载ueditor或neditor源码,拷贝到static目录下面 [img]http://files.jb51.net/file_images/article/201711/2017111493852562.png?201710149391[/img] 然后修改ueditor.config.js配置文件 [img]http://files.jb51.net/file_images/article/201711/2017111493911535.png?2017101493918[/img] 在vue项目的main.js添加ueditor引用 [img]http://files.jb51.net/file_images/article/201711/2017111493936323.png?2017101493945[/img] 新建3个页面 home,tab1,tab2。tab1和tab2是home下面的子页面 [img]http://files.jb51.net/file_images/article/201711/2017111494047960.jpg?2017101494110[/img] 在router-view外面一定要添加keep-alive组件和transition组件,不然ueditor实例无法保存 在components文件夹下面新建一个editor作为编辑器的公共组件 在tab1中调用editor,同时要传入一个id并在editor页面接受,注意如果需要多个实例,id一定不能相同
 <template>
 <div>
 <editor ref="editor" id="tab1Editor"></editor>
 <button @click="getContent" class="m-t-10">获取内容</button>
 <div>
 <span>当前富文本编辑器内容是: {{content}}</span>
 </div>
 </div>
 </template>

 <script>
 import Editor from '@/components/editor'
 export default {
 name: 'tab1',
 components: { Editor },
 data() {
 return {
 content:''
 }
 },
 methods: {
 //获取内容
 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里面调用ueditor的destroy方法,确保每次离开页面的时候 会销毁编辑器实例,这样就可以渲染多个ueditor实例了,并且每次切换都能保存编辑器的内容。 如果多个tab只需要一个实例请调用reset()方法
 <template>
 <div>
 <div :id="this.id"></div>
 </div>
 </template>

 <script>
 export default {
 name: 'editor',
 props: ['id'],
 data() {
 return {
 ue: '', //ueditor实例
 content: '', //编辑器内容
 }
 },
 methods: {
 //初始化编辑器
 initEditor() {
 this.ue = UE.getEditor(this.id, {
 initialFrameWidth: '100%',
 initialFrameHeight: '350',
 scaleEnabled: true
 })
 //编辑器准备就绪后会触发该事件
 this.ue.addListener('ready',()=>{
 //设置可以编辑
 this.ue.setEnabled()
 })
 //编辑器内容修改时
 this.selectionchange()
 },
 //编辑器内容修改时
 selectionchange() {
 this.ue.addListener('selectionchange', () => {
 this.content = this.ue.getContent()
 })
 }
 },
 activated() {
 //初始化编辑器
 this.initEditor()
 },
 deactivated() {
 //销毁编辑器实例,使用textarea代替
 this.ue.destroy()
 //重置编辑器,可用来做多个tab使用同一个编辑器实例
 //如果要使用同一个实例,请注释destroy()方法
 //this.ue.reset()
 }
 }
 </script>
[b]源码地址[/b] github:[url=https://github.com/oblivioussing/vue-ueditor-multi]https://github.com/oblivioussing/vue-ueditor-multi[/url] 本地下载:[url=http://xiazai.jb51.net/201711/yuanma/vue-ueditor-multi(jb51.net).rar]http://xiazai.jb51.net/201711/yuanma/vue-ueditor-multi(jb51.net).rar[/url] [b]总结[/b] 以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对编程素材网的支持。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部