vue中使用wangEditor富文本编辑器(踩坑)

一、wangEditor介绍及使用

wangEditor —— 轻量级 web 富文本编辑器,配置方便,使用简单。支持 IE10+ 浏览器。

  请根据 具体 版本查看对应版本的 文档 v4、v5用法不一样!

下载

使用

二、vue中使用

1.npm install wangeditor (注意 wangeditor 全部是小写字母

2.引入import E from "wangeditor"; //引入富文本插件

3.创建实例

var editor = new E('#editor')
// 或者 var editor = new E(document.getElementById('editor'))
//事实上传入类名也是可以的var editor = new E(document.getElementsByClassName(editor))
//应该document.getElement开头的都可以但是一般没人这么干
 editor.create()

new E()传入挂载的地方

create()创建

一个页面有多个

 var editor1 = new E('#div1', '#div2')
 editor1.create()

更多说明以及API请看wangEditor官方文档

三、踩坑即报错

1.可能会遇到的报错

这个错说明你实例要挂载的dom还不存在

2.当你需要让这个富文本显示与隐藏(v-show)时可能会存在创建多个实例

其实这个只需要清除让当前挂载元素的innerHtml清空即可。 

四、循环创建富文本以及多个富文本实例叠加解决方案

场景:比如评论列表点击回复出在下面出现一个富文本框

1.如果使用v-show来实现

重点是要清除

 ele.innerHTML = "";

<template>
  <div class="hello">
        <div v-for="item in items" :key="item.id" v-if="first">
          <p>{{item.message}}</p>
          <button @click="a(item)">回复</button>
          <div :id="item.id" v-show="item.show"></div>
        </div>
  </div>
</template>

<script>
import E from "wangeditor"; //引入富文本插件
export default {
  name: "HelloWorld",
  data() {
    return {
      items: [
        //原数组
        { message: "Foo", show: false, id: 1 },
        { message: "Bar", show: false, id: 2 },
      ]
    };
  },
  methods: {
    a(i) {
      //循环遍历
      var arr = this.items.map((item, index) => {
        //点击获取的id和原来的数据比较
        if (item.id == i.id) {
          //获取富文本挂再节点
          var ele = document.getElementById(item.id);
          //创建富文本
          var editor = new E(ele);
          if (i.show) {
            item.show = false;
            if (ele) {
              //清空挂载节点
              ele.innerHTML = "";
            }
          } else {
            editor.create();
            item.show = true;
          }
        }
        return item;
      });
      //赋值
      this.items = arr;
    },
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>

2.v-if实现

重点是创建需要在this.$nextTick里创建

<template>
  <div class="hello">
        <div v-for="item in items" :key="item.id" v-if="first">
          <p>{{item.message}}</p>
          <button @click="a(item)">回复</button>
          <div :id="item.id" v-if="item.show"></div>
        </div>
  </div>
</template>

<script>
import E from "wangeditor"; //引入富文本插件
export default {
  name: "HelloWorld",
  data() {
    return {
      items: [
        //原数组
        { message: "Foo", show: false, id: 1 },
        { message: "Bar", show: false, id: 2 },
      ]
    };
  },
  methods: {
    a(i) {
    var arr = this.items.map((item, index) => {
        if (item.id == i.id) {
          if (i.show) {
            item.show = false;
          } else {
            item.show = true;
             this.$nextTick(() => { //因为v-if需要立即拿到节点这里是重点必须写到$nextTick
              var ele = document.getElementById(item.id);
              var editor = new E(ele);
              editor.create();
             });
          }
        }
        return item;
      });
      this.items = arr;
    },
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>

可以根据你的需求来选择实现方式。

五、总结

wangEditor是一个轻量级的富文本编辑器插件,不仅能在vue中使用还可以在react/Angular中使用。并且以上方法在react中稍微变通也可以使用。

其它 还有 富文本 编辑器 :

vue-quill-editor 

https://www.kancloud.cn/liuwave/quill/1409423icon-default.png?t=N7T8https://www.kancloud.cn/liuwave/quill/1409423
TinyMCE
http://tinymce.ax-z.cn/icon-default.png?t=N7T8http://tinymce.ax-z.cn/

好的,我可以为您介绍一下使用Vue3封装WangEditor富文本组件的步骤。 首先,我们需要安装WangEditorVue3,可以通过以下命令进行安装: ```bash npm install wangeditor@latest npm install vue@next ``` 接着,我们可以在Vue3的组件使用WangEditor。下面是一个简单的示例: ```vue <template> <div ref="editorElem"></div> </template> <script> import WangEditor from 'wangeditor' export default { mounted() { const editor = new WangEditor(this.$refs.editorElem) editor.create() } } </script> ``` 在上面的代码,我们通过import导入了WangEditor,并在mounted钩子函数创建了一个新的编辑器实例。注意,我们需要在组件的模板添加一个ref属性,用于引用编辑器的DOM元素。 如果您需要进一步封装WangEditor组件,可以考虑将其封装为一个Vue组件,以便在其他地方重复使用。下面是一个简单的示例: ```vue <template> <div :id="editorId"></div> </template> <script> import WangEditor from 'wangeditor' export default { props: { value: String, placeholder: String, height: { type: String, default: '300px' } }, data() { return { editorId: `editor-${Math.random().toString(36).substr(2, 9)}`, editor: null } }, mounted() { this.editor = new WangEditor(`#${this.editorId}`) this.editor.config.height = this.height this.editor.config.placeholder = this.placeholder this.editor.config.onchange = this.handleChange this.editor.create() this.editor.txt.html(this.value) }, methods: { handleChange() { this.$emit('input', this.editor.txt.html()) } }, beforeUnmount() { this.editor.destroy() } } </script> ``` 在上面的代码,我们定义了一个WangEditor组件,并通过props接收了一些参数,包括组件的初始值、占位符和高度等。在mounted钩子函数,我们创建了一个新的编辑器实例,并通过config属性设置了一些编辑器的配置项,包括高度、占位符和内容变化时的回调函数等。我们还通过handleChange方法监听了编辑器内容的变化,并通过$emit方法向父组件发送了一个input事件,以便在父组件更新组件的绑定值。最后,我们在beforeUnmount钩子函数销毁了编辑器实例,以避免内存泄漏。 使用时,您可以像使用其他自定义组件一样,在Vue3的模板引用WangEditor组件,并通过v-model指令绑定组件的值: ```vue <template> <div> <wangeditor v-model="content" placeholder="请输入内容" height="500px" /> <div>{{ content }}</div> </div> </template> <script> import WangEditor from '@/components/WangEditor.vue' export default { components: { WangEditor }, data() { return { content: '' } } } </script> ``` 在上面的代码,我们通过import导入了WangEditor组件,并在模板引用了该组件。我们还通过v-model指令绑定了组件的值,以便在父组件获取和更新该值。
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

崽崽的谷雨

漫漫前端路,摸爬滚打

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值