Vue集成WangEditor的初次使用

本文介绍了在Vue项目中集成WangEditor的过程,包括将其作为组件使用、父子模块通信以及图片上传的方法。通过创建局部组件并在NewsAdd.vue中引入,实现了富文本编辑功能。重点讲解了如何通过事件总线实现父子组件间的数据同步,以及前端和后端的图片上传配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

作为一个菜鸟表示真的很难吖。
这个插件就一个字:小巧!好用!
首先我是Vue+springboot的前后端分离
所以在查的时候结合文档和网上的一些例子,在此感谢帮助到我的大佬们!
因为富文本编辑框之后会在很多地方用到,所以我采用了把WangEditor单独拎出来做一个组件然后用父子模块通信的方式来实现富文本的编辑。
项目的文件结构如图:
在这里插入图片描述
然后这个WangEditor.vue可以把它理解成子模块,在新闻添加的vue文件
NewsAdd.vue里引入使用这个子模块,故NewsAdd.vue理解为父模块
下面是WangEditor.vue 源码

<template>
  <div>
    <div ref="editor" style="text-align:left"></div>
    <button v-on:click="getContent">查看内容</button>
  </div>
</template>

<script>
  import E from 'wangeditor'

  export default {
    name: "WangEditor",
    data() {
      return {
        // 真正的编辑器里的内容
        editorContent: '',
        // 编辑器对象
        editor: null
      }
    },
    watch: {
      //  watch 表示监听  当父组件的内容变化时需要更新编辑器的内容
      content() {
        this.editor.txt.html(this.content)
      }
    },
    /**
     * 这里使用了 Vue 的 mounted 函数钩子,这属于 Vue 生命周期的一个阶段
     */
    mounted() {
      var editor = new E(this.$refs.editor)
      // 当编辑器内容变化的时候通知父组件
      editor.customConfig.onchange = (html) => {
        this.editorContent = html
        // 通知父级控件方法,富组件可以通过 editorContent 事件去获取最新的编辑器内容
        this.$emit('editorContent', html)
      }
      // 下面两个配置,使用其中一个即可显示“上传图片”的tab。但是两者不要同时使用!!!
      // editor.customConfig.uploadImgShowBase64 = true   // 使用 base64 保存图片
      editor.customConfig.uploadImgServer = 'http://localhost:8081/uploadImg'  // 上传图片到服务器
      //图片的大小和数量都是用默认值  5M和1000张
      // 将图片大小限制为 3M
      // editor.customConfig.uploadImgMaxSize = 3 * 1024 * 1024
      // 限制一次最多上传 5 张图片
      // editor.customConfig.uploadImgMaxLength = 5
      //上传图片时,可自定义filename,即在使用formdata.append(name, file)添加图片文件时,自定义第一个参数。
      editor.customConfig.uploadFileName = 'myFileName'
      //可使用监听函数在上传图片的不同阶段做相应处理
      editor.customConfig.uploadImgHooks = {
        // 图片上传并返回结果,图片插入成功之后触发
        // xhr 是 XMLHttpRequst 对象,editor 是编辑器对象,result 是服务器端返回的结果
        success: function (xhr, editor, result) {

        },
        customInsert: function (insertImg, result, editor) {
          var url =result.data;//获取后台返回的url
          insertImg(url);
        }
      }


      editor.create()
      editor.txt.html(this.content)
    },
    props: {
      // 传递过来的编辑器内容参数,用于设置编辑器内容
      content: {
        type: String,
        default: ''
      }
   
### Vue集成WangEditor富文本编辑器 #### 安装配置 为了在Vue项目中集成WangEditor富文本编辑器,首先需要安装`@wangeditor/editor`以及其对应的样式包。可以通过npm来完成这一步骤。 ```bash npm install @wangeditor/editor --save npm install @wangeditor/editor-for-vue --save ``` 接着,在项目的入口文件(通常是main.js),引入必要的CSS资源[^2]: ```javascript import &#39;@wangeditor/editor/dist/css/style.css&#39; ``` #### 使用方法 创建一个新的组件用于初始化编辑器实例,并将其挂载至页面上指定的位置。下面是一个简单的例子说明如何操作: ```html <template> <div id="editor-container"></div> </template> <script> // 导入 Editor 和 createEditor 函数 import { createEditor } from &#39;@wangeditor/editor&#39; export default { name: &#39;MyEditor&#39;, data() { return { editor: null, } }, mounted() { this.editor = createEditor({ selector: &#39;#editor-container&#39;, // 编辑区域的选择器 config: {}, // 配置项 mode: &#39;default&#39; // 模式,默认即可 }) }, beforeDestroy() { if (this.editor == null) return; this.editor.destroy(); // 组件销毁前要记得释放掉编辑器实例 } } </script> ``` 对于更复杂的场景比如自定义上传图片等功能,则可以在config参数里设置相应的回调函数处理逻辑。 #### 示例 这里给出一段完整的代码片段作为参考,展示了怎样在一个标准的Vue单文件组件内使用WangEditor编辑器,并实现了基本的文字输入功能。 ```html <!-- MyRichTextEditor.vue --> <template> <div class="richText"> <!-- 这里放置实际渲染出来的编辑框 --> <div ref="toolbar" style="border-bottom: 1px solid #ccc;"></div> <div ref="editor" style="text-align:left;min-height:300px;border:1px dashed #ccc;margin-top:-1px;padding-left:8px;"></div> </div> </template> <script> import WangEditor from &#39;@wangeditor/editor&#39;; import &#39;@wangeditor/editor/dist/css/style.css&#39;; export default { props: [&#39;value&#39;], watch: { value(newValue, oldValue){ const editorValue = this.editor.txt.html(); if(editorValue !== newValue && !this.isChangeByInner){ this.editor.txt.html(newValue); } } }, methods:{ changeHandler(newHtml){ this.$emit(&#39;input&#39;, newHtml); } }, mounted(){ var _self=this; // 创建工具栏和编辑区对象 let E = WangEditor; this.editor = new E(this.$refs.editor); // 设置 onchange 回调函数,将数据同步到父级组件 this.editor.config.onchange = function(html){ _self.changeHandler(html); }; // 自定义菜单配置... // 创建编辑器 this.editor.create(); // 初始化内容 setTimeout(() => { this.editor.txt.html(_self.value || &#39;&#39;); }, 500); }, destroyed(){ this.editor.destroy(); } }; </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值