vue2.0项目中使用Ueditor富文本编辑器示例

最近在vue项目中需要使用富文本编辑器,于是将Ueditor集成进来,作为公共组件。
在线预览:https://suweiteng.github.io/vue2-management-platform/#/editor
项目地址:https://github.com/suweiteng/vue2-management-platform 记得在右上角点个赞哦~

1.放入静态资源并配置

首先把官网下载的Ueditor资源,放入静态资源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.开发公共组件

开发公共组件,可设置填充内容defaultMsg,配置信息config(宽度和高度等),并提供获取内容的方法。

<template>
  <div>
    <script id="editor" type="text/plain"></script>
  </div>
</template>
<script>
  export default {
    name: 'UE',
    data () {
      return {
        editor: null, id: Math.random().toString(16).substring(2) + 'ueditorId'

      }
    },
    props: {
      defaultMsg: {
        type: String
      },
      config: {
        type: Object
      }
    },
    mounted() {
      const _this = this;
      this.$refs.editor.id = this.id;
      this.editor = UE.getEditor(this.id, this.config); // 初始化UE
      this.editor.addListener("ready", function () {
        _this.editor.setContent(_this.defaultMsg); // 确保UE加载完成后,放入内容。
      });
    },
    methods: {
      getUEContent() { // 获取内容方法
        return this.editor.getContent()
      }
    },
    destroyed() {
      this.editor.destroy();
    }
  }
</script>

4.使用

当我们需要使用富文本编辑器时,直接调用公共组件即可

<template>
  <div class="components-container">
    <div class="info">UE编辑器示例<br>需要使用编辑器时,调用UE公共组件即可。可设置填充内容defaultMsg,配置信息config(宽度和高度等),可调用组件中获取内容的方法。</div>
    <button @click="getUEContent2()">获取内容</button>
    <div class="editor-container">
      <UE :defaultMsg=defaultMsg :config=config ref="ue1"></UE>
    </div>
<button @click="getUEContent2()">获取内容</button>    <div class="editor-container">           <UE :defaultMsg=defaultMsg :config=config ref="ue2"></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: {
      getUEContent2() {
        let content = this.$refs.ue.getUEContent();
        
        console.log(content)
      }, getUEContent2() {        let content = this.$refs.ue.getUEContent();                console.log(content)      }
    }
  };
</script>

效果如下:




5.报错

ESlint报错

eslint报错的参考请评论4L 5L

严格模式报错

部分人使用时出现以下报错:
Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them...
这个问题是因为项目中的使用的babel默认添加了use strict造成,可参考 https://segmentfault.com/q/1010000007415253
我采用的是链接中答案的第三种方式:添加了babel-plugin-transform-remove-strict-mode,并在.babelrc里添加下列代码

{
  "plugins": ["transform-remove-strict-mode"]
}

然后就没问题了。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
为了在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`变量来获取编辑器内容,以便将其保存到后端或进行其他操作。 希望这个示例对你有所帮助!
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值