在vue中创建多个ueditor实例

简介

在vue中创建多个ueditor实例,我使用neditor,其实就是把ueditor样式美化了下,其他和ueditor几乎一样

截图

show.gif

源码地址

https://github.com/obliviouss...

说明

下载ueditor或neditor源码,拷贝到static目录下面

in-static.png

然后修改ueditor.config.js配置文件

config.png

在vue项目的main.js添加ueditor引用

vue-main.png

新建3个页面 home,tab1,tab2。tab1和tab2是home下面的子页面

vue-home.png

在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>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为了在Vue项目使用UEditor,你需要先安装UEditor的npm包。你可以使用以下命令进行安装: ``` npm install vue-ueditor-wrap --save ``` 安装完成后,你可以在Vue组件引入UEditor并使用。下面是一个简单的示例: ```html <template> <div> <vue-ueditor-wrap :options="editorConfig" v-model="content"></vue-ueditor-wrap> </div> </template> <script> import VueUeditorWrap from 'vue-ueditor-wrap' export default { components: { VueUeditorWrap }, data() { return { content: '', editorConfig: { UEDITOR_HOME_URL: '/static/UEditor/', // UEditor资源文件的根目录 serverUrl: '/api/upload', // UEditor上传图片和文件的后端接口地址 toolbars: [ [ 'undo', 'redo', '|', 'bold', 'italic', 'underline', 'strikethrough', 'removeformat', '|', 'insertorderedlist', 'insertunorderedlist', '|', 'link', 'unlink', 'anchor', '|', 'blockquote', 'pasteplain', '|', 'forecolor', 'backcolor', '|', 'justifyleft', 'justifycenter', 'justifyright', 'justifyjustify', '|', 'fullscreen' ] ], } } } } </script> ``` 这个示例,我们首先在组件引入了`vue-ueditor-wrap`组件。然后我们在`data`定义了一个`content`变量来保存UEditor编辑器的内容,并定义了一个`editorConfig`对象来配置UEditor。 最后,我们在模板使用`vue-ueditor-wrap`组件,将它的`options`属性设置为我们定义的`editorConfig`对象,并使用`v-model`指令将编辑器的内容绑定到`content`变量上。 当用户在UEditor编辑内容时,`content`变量的值也会相应地更新。你可以在组件使用`content`变量来获取编辑器的内容,以便将其保存到后端或进行其他操作。 希望这个示例对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值