Vue项目之富文本编辑器wangEditor的change

项目中用到了富文本编辑器,看到很多网友都推荐了 wangEditor,所以就拿过来用了。在使用过程中也遇到了一些小坑,今天就来分享一下,希望可以帮到也遇到一样的坑的你。(另外选择这个编辑器的原因是:之前看过作者王福朋的一些博客github,对本人帮助挺大的,如果你还走在前端学习路上,推荐看一下他的博客)

NO.1 关于配置项

官方API 和 另外一些人写得比较详细,可以搜索一下,比较容易。

NO.2 用事件触发change,将编辑器中的内容传给父组件

项目中需求是这样:需要增加快捷回复的功能(宏),就是输入特定的开始字符(比如 ##),然后出现快捷回复列表,列表中是匹配的那些短语,当鼠标点击那些短语的时候,将整段内容输入到编辑器中。

OK,开始做。我是将编译器再次封装成一个组件,然后watch中监听内容改变,通过 $emit 实时将内容发送给父组件。结果是:编译器中有了内容,不会马上发送给父组件,需要在编辑器中再输入空格或者回车。这样肯定是有bug呀!然后就是翻api,百度,google。。。

最后,终于被我找到了一个偏方(中间一度想着不行换个编译器吧 ?),那个泪呀。

重点:就是在通过点击快捷回复列表输入内容的事件中,为编译器增加一个点击事件。

下面是图片和代码:

HTML代码:

<div id="emailDitor" ref="AEditor"></div>

 JavaScript代码:

clickInputComplete($event){
      // 处理编译器不自动触发onchange 事件的偏方
      $('#emailDitor p').click();
      // 隐藏快捷回复的框
      $('.auto-complete-email-textarea').hide();
    }

补充:我是在点击输入快捷回复时增加的点击编辑器的事件,大家视情况而定,就是在需要触发编辑器change事件的地方增加一个点击事件

想想也是够了是不是,好狗血。如果你还有使用这个编译器的其他问题,欢迎留言。

参考 eeeeeeeason 的回答:链接如下:

https://github.com/wangfupeng1988/wangEditor/issues/1749

好的,我可以给你提供一个基于 vue2.0 的富文本编辑器的实现思路。 首先,我们需要引入一个富文本编辑器的库,比较常用的有Quill、WangEditor、TinyMCE等。这里以Quill为例进行实现。 1. 安装Quill ``` npm install quill --save ``` 2. 在Vue组件中引入Quill ```vue <template> <div id="editor"></div> </template> <script> import Quill from 'quill' export default { mounted() { this.quillEditor = new Quill('#editor', { modules: { toolbar: [ ['bold', 'italic', 'underline', 'strike'], // 加粗、斜体、下划线、删除线 ['blockquote', 'code-block'], // 引用、代码块 [{ 'header': 1 }, { 'header': 2 }], // 标题 [{ 'list': 'ordered' }, { 'list': 'bullet' }], // 列表 [{ 'script': 'sub' }, { 'script': 'super' }], // 上下标 [{ 'indent': '-1' }, { 'indent': '+1' }], // 缩进 [{ 'direction': 'rtl' }, { 'align': [] }], // 对齐方式 [{ 'color': [] }, { 'background': [] }], // 颜色、背景色 ['image', 'video'], // 图片、视频 ['clean'] // 清除格式 ] }, placeholder: '请输入内容', theme: 'snow' // 主题,有两个值可选:snow和bubble }) } } </script> <style> /* Quill需要的CSS */ @import "~quill/dist/quill.snow.css"; </style> ``` 这样就可以得到一个基本的富文本编辑器了。但是这个富文本编辑器不能与Vue组件的数据进行双向绑定,我们需要对Quill进行一些扩展。 3. 扩展Quill 我们需要在Quill的change事件中,把编辑器中的内容同步到Vue组件的data中。同时,当Vue组件的data中的值发生变化时,要把新的值同步到Quill中。 ```vue <template> <div id="editor" ref="editor"></div> </template> <script> import Quill from 'quill' export default { data() { return { content: '' } }, mounted() { this.quillEditor = new Quill(this.$refs.editor, { modules: { toolbar: [ ['bold', 'italic', 'underline', 'strike'], // 加粗、斜体、下划线、删除线 ['blockquote', 'code-block'], // 引用、代码块 [{ 'header': 1 }, { 'header': 2 }], // 标题 [{ 'list': 'ordered' }, { 'list': 'bullet' }], // 列表 [{ 'script': 'sub' }, { 'script': 'super' }], // 上下标 [{ 'indent': '-1' }, { 'indent': '+1' }], // 缩进 [{ 'direction': 'rtl' }, { 'align': [] }], // 对齐方式 [{ 'color': [] }, { 'background': [] }], // 颜色、背景色 ['image', 'video'], // 图片、视频 ['clean'] // 清除格式 ] }, placeholder: '请输入内容', theme: 'snow' // 主题,有两个值可选:snow和bubble }) // 把编辑器中的内容同步到Vue组件的data中 this.quillEditor.on('text-change', () => { this.content = this.quillEditor.root.innerHTML }) // 把Vue组件的data中的值同步到Quill中 this.$watch('content', (newValue) => { this.quillEditor.root.innerHTML = newValue }) } } </script> <style> /* Quill需要的CSS */ @import "~quill/dist/quill.snow.css"; </style> ``` 现在,我们就可以在Vue组件中使用这个富文本编辑器,并且能够与Vue组件的data进行双向绑定了。 ```vue <template> <div> <editor v-model="content"></editor> <div v-html="content"></div> </div> </template> <script> import Editor from './Editor.vue' export default { components: { Editor }, data() { return { content: '' } } } </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值