vue中封装wang-editor组件和自定义音频组件

本文介绍如何在Vue项目中封装wang-editor富文本编辑器,并实现自定义的音频组件。通过封装,可以更好地集成到Vue应用中,提供便捷的文本编辑功能和音频插入能力。

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

wang-editor.vue

<template>
<div>
  <div :id="params.id">
  </div>
  <Audio v-if="audioShow" @uploadAudio="uploadAudio"/>
</div>
</template>
<script>
import E from 'wangeditor'
import Audio from './audio.vue'
export default {
  name: 'wangEditor',
  components: {
    Audio
	},
  props: {
    id: {
      type: String,
      default: () => ''
    },
    content: {
      type: String,
      default: () => ''
    },
  },
  watch: {
    id (newVal) {
      this.params.id = newVal
    }
  },
  data() {
    return {
      localEditor: null,
      audioShow: false,
      params: {
        id: 'wang-editor', //富文本框ID
        menus: [
          'bold',
          'head',
          'link',
          'italic',
          'underline'
        ],
        customImgUpload: {
          flag: true, //是否自定义方法上传图片
          methods: {
            customUploadImg: (resultFiles, insertImgFn) => {
              console.log(resultFiles, insertImgFn, '插入图片')
              this.uploadFile('img', resultFiles, insertImgFn)
              // resultFiles 是 input 中选中的文件列表
              // insertImgFn 是获取图片 url 后,插入到编辑器的方法
              // 上传图片,返回结果,将图片插入到编辑器中
            }
          }
        },
        customVideoUpload: {
          flag: true, //是否自定义方法上传视频
          methods: {
            customUploadVideo: (resultFiles, insertVideoFn) => {
              console.log(resultFiles, insertVideoFn, '插入视频')
              this.uploadFile('video', resultFiles, insertVideoFn)
              // resultFiles 是 input 中选中的文件列表
              // insertVideoFn 是获取视频 url 后,插入到编辑器的方法
              // 上传视频,返回结果,将视频地址插入到编辑器中
              // insertVideoFn('https://touxianglogo.com/img/3.36eb1cce.jpg')
            }
          }
        },
        params: {
          focus: false, //自动focus
          placeholder: '请输入正文...', //placeholder内容提示
          showMenuTooltips: true, //菜单栏提示
          menuTooltipPosition: 'down',
          excludeMenus: [
            //自定义程度比较轻时
### Vue2 中使用 wangeditor 实现双向绑定 为了在 Vue2 项目中正确使用 `wangeditor` 并实现数据的双向绑定,可以采用自定义组件的方式处理。通过监听编辑器的内容变化并手动更新父级组件的数据,以此模拟 `v-model` 的行为。 #### 创建富文本编辑器组件 首先创建一个新的 Vue 组件用于封装 `wangeditor`: ```javascript // components/MyEditor.vue <template> <div ref="editorElem"></div> </template> <script> import E from 'wangeditor'; export default { props: ['value'], data() { return { editor: null, content: this.value || '' }; }, watch: { value(newValue) { if (newValue !== this.editor.txt.html()) { this.editor.txt.html(newValue); } } }, mounted() { const editor = new E(this.$refs.editorElem); Object.assign(editor.config, { onchange: function () { this.content = editor.txt.html(); this.$emit('input', this.content); // 更新父组件中的值 }.bind(this), }); editor.create(); this.editor = editor; }, }; </script> ``` 此代码片段展示了如何初始化 `wangeditor` 设置配置项[^1]。注意这里利用了 `this.$emit('input')` 来通知外部组件内容已更改,这正是模仿 `v-model` 行为的关键部分。 #### 使用 MyEditor 组件 接着可以在其他地方引入该组件,并像下面这样使用它: ```html <!-- parentComponent.vue --> <template> <div class="container"> <!-- ... --> <my-editor v-model="articleContent"/> <!-- ... --> </div> </template> <script> import MyEditor from './components/MyEditor.vue'; export default { name: "ParentComponent", components: { MyEditor }, data(){ return{ articleContent:'' } } } </script> ``` 上述例子说明了怎样在一个更大的应用程序上下文中应用新构建好的 `MyEditor` 组件[^2]。由于实现了类似于 `v-model` 的接口设计模式,因此可以直接将其作为标准表单控件一样对待。 #### 处理初始加载时的数据回显 如果希望在页面初次渲染的时候也能显示已有内容,则可以通过传递默认HTML字符串给子组件来达到目的: ```html <MyEditor :value="existingArticleContent" @input="(newVal)=>{ existingArticleContent=newVal }"/> ``` 这段代码确保了即使是在首次访问页面的情况下,也可以正常展示之前保存过的文章正文[^3]。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值