Vue使用UEditor富文本编辑器

UEditor是一个功能强大、兼容性比较好的富文本编辑器,大概是长成以下的样子:

要在Vue的webpack项目中使用UEditor,具体的步骤如下:

一、下载UEditor源码包

1、到https://ueditor.baidu.com/website/download.html下载相应的版本

2、把下载好的包放在项目的目录中(我放在static目录下)

二、修改文件参数

打开/static/UM/umeditor.config.js文件,加入一行参数:

window.UMEDITOR_HOME_URL = '/static/UE/'

三、引入文件

1、下载Jquery的文件,我把文件放在/static目录下:

因为UEditor插件是依赖到Jquery的,所以在目录的index.html文件中,引入Jquery文件:

2、引入UEditor相关文件

在main.js文件中引入:

import '../static/UM/umeditor.config.js'

import '../static/UM/umeditor.min.js'

import '../static/UM/lang/zh-cn/zh-cn.js'

import '../static/UM/themes/default/css/umeditor.min.css'

注意引入的顺序,引入之后window.UM就是UEditor的对象了。

四、封装一个组件

在components下新建一个.vue文件,代码写成这样:

<template>
  <div>
    <script id="editor" type="text/plain"></script>
  </div>
</template>
<script>
  export default {
    name: 'UM',
    data () {
      return {
        editor: null
      }
    },
    props: {
      defaultMsg: {
        type: String
      },
      config: {
        type: Object
      }
    },
    mounted() {
      const _this = this;
      this.editor = UM.getEditor('editor', this.config); 
      this.editor.ready(() => {
        this.editor.setContent(this.defaultMsg);
      });
    },
    methods: {
      getContent() { 
        return this.editor.getContent()
      }
    },
    destroyed() {
      this.editor.destroy();
    }
  }
</script>

五、使用组件

可以在需要用到编辑器的地方,引入组件:

<template>
    <div>
        <UM :defaultMsg="defaultMsg" :config="config" ref="um"></UM>
    </div>
</template>

<script>
  import UM from '@/components/editor.vue';
  export default {
    data() {
        return {
             defaultMsg: '初始文字',

             config: {
                 initialFrameWidth: null,
                 initialFrameHeight: 350
             }
        }
    
    }
    components: {
      UM
    }

   }
</script>

defaultMsg:初始的文本内容;

config: 编辑器相应的配置;

this.$refs.um.getContent():获取文本的内容。

根据以上的步骤就可以在Vue项目使用UEditor,具体对编辑器的参数修改或操作处理,请查看官方API。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值