Vue.js集成UEditor富文本编辑器的封装与使用指南

作者:组件花园

时间:2026-10-22

浏览:171

本文详细讲解了如何在Vue.js项目中集成百度UEditor富文本编辑器。通过将UEditor封装为独立的.vue组件,利用props传递配置与初始值,并通过自定义事件同步内容变更,实现了代码复用与模块化。文章涵盖了从引入核心JS/CSS文件、配置config.js到创建编辑器模板及使用父组件调用的完整步骤,为Vue开发者提供了具体的实践方案。

文章目录

Vue集成UEditor的背景与思路

在前端开发过程中,经常需要在网页中集成富文本编辑器。此前,公司在Vue.js项目中需要接入UEditor,但网上缺乏详细的操作说明。经过一天的摸索与尝试,最终成功实现了集成,现将具体过程整理如下。

1. 总体思路

1.1 模块化

Vue的一大优势在于其模块化特性,这有助于实现页面逻辑的复用。因此,我们可以将UEditor重新封装为一个.vue模板文件。其他组件只需引入该模板,即可实现代码复用,避免重复编写。

1.2 数据传输

在数据交互方面,父组件需要通过props传递编辑器的长度、宽度以及初始文本等配置信息。同时,编辑器内容的变化可以通过Vue的自定义事件机制向父组件传递,确保数据同步。

UEditor在Vue中的具体实现步骤

2. 具体实现步骤

2.1 引入关键的JS以及CSS文件

首先,需要将UEditor相关的核心文件全部拷贝到项目目录中。

Vue集成UEditor的背景与思路

2.2 配置Ueditor.config.js

在配置文件中,首要任务是设置URL参数,将其指向刚才拷贝的文件根目录。建议在此处使用相对路径,以确保路径解析的准确性。


var URL = window.UEDITOR_HOME_URL || '/static/ueditor/';

接着,可以设置编辑器的默认宽度和高度。


,initialFrameWidth:null // null表示宽度自动
,initialFrameHeight:320

其他功能配置可参考官方文档进行设置。

2.3 创建编辑器模板

在编辑器模板中,需要import Ueditor的核心JS库,并添加contentChange回调函数,即可完成编辑器的基本封装。

采用import语法引入核心JS库符合ES6模块化规范。虽然网上有建议在main.js中全局引入,但这可能导致页面首次加载速度变慢,因此局部按需引入更为合适。


<template>
 <div ref="editor"></div>
</template>

<script>
 /* eslint-disable */
 import '../../../assets/js/ueditor/ueditor.config';
 import '../../../assets/js/ueditor/ueditor.all';
 import '../../../assets/js/ueditor/lang/zh-cn/zh-cn';

 import { generateRandonInteger } from '../../../vuex/utils';

 export default {
 data() {
  return {
  id: generateRandonInteger(100000) + 'ueditorId',
  };
 },
 props: {
  value: {
  type: String,
  default: null,
  },
  config: {
  type: Object,
  default: {},
  }
 },
 watch: {
  value: function value(val, oldVal) {
  this.editor = UE.getEditor(this.id, this.config);
  if (val !== null) {
   this.editor.setContent(val);
  }
  },
 },
 mounted() {
  this.$nextTick(function f1() {
  // 保证 this.$el 已经插入文档

  this.$refs.editor.id = this.id;
  this.editor = UE.getEditor(this.id, this.config);

  this.editor.ready(function f2() {
   this.editor.setContent(this.value);

   this.editor.addListener("contentChange", function () {
   const wordCount = this.editor.getContentLength(true);
   const content = this.editor.getContent();
   const plainTxt = this.editor.getPlainTxt();
   this.$emit('input', { wordCount: wordCount, content: content, plainTxt: plainTxt });
   }.bind(this));

   this.$emit('ready', this.editor);
  }.bind(this));
  });
 },
 };
</script>

<style>
 body{
  background-color:#ff0000;
 }
</style>

编辑器组件的使用与注意事项

3. 编辑器的使用

在使用封装好的编辑器模板时,需要通过props传入config配置对象以及初始文本value。


<template xmlns:v-on="http://www.w3.org/1999/xhtml">
 <div class="edit-area">
  <ueditor v-bind:value=defaultMsg v-bind:config=config v-on:input="input" v-on:ready="ready"></ueditor>
 </div>

</template>

<script>
 import ueditor from './ueditor.vue';

 export default {
 components: {
  ueditor,
 },
 data() {
  return {
  defaultMsg: '初始文本', 
  config: {
   initialFrameWidth: null,
   initialFrameHeight: 320,
  },
  };
 },
 };
</script>

若需支持图片上传功能,还需在后台配置相应的接口。由于时间限制,这部分内容暂未深入研究,后续将补充相关说明。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持本站。

实用工具推荐

电子包浆

绿化、压缩、噪点和虚拟水印,自由调节做旧程度。

使用 ›
图片压缩

选择画质和输出格式,预览压缩前后的大小。

使用 ›
相关文章