VUE结合富文本wangeditor,实现包含本地图片上传至内容中,操作成功后清空富文本

近日在写一个小的项目时,需要用到富文本,通过VUE结合富文本进行实现,网上知名的富文本不是太多,经过多个富文本的选择对比,最后决定用wangeditor这个富文本(官网示例)。如何初始化一个富文本,

前提:需要引入wangeditor:

cnpm install wangeditor

且看:

<template>
    <div>
        <div id="editorElem" ref="editor" style="text-align:left"></div>
        <Button shape="circle" type="primary" v-on:click="getContent">submit</Button>
    </div>
</template>
<script>
import E from 'wangeditor'
 export default {
      name: 'editor',
      data () {
        return {
            editor: '',
            editorContent: ''
        }
      },
      methods: {
        getContent: function () { 
            console.log(this.editorContent) //获取富文本内容
            this.editor.txt.clear()  //清空富文本的内容
        }
      },
     mounted() {
        // var editor = new E('#editorElem')
        this.editor = new E(this.$refs.editor)
        this.editor.customConfig.uploadImgShowBase64 = true //图片以base64形式保存
        this.editor.customConfig.onchange = (html) => {
          this.editorContent = html
        }
        this.editor.customConfig.pasteTextHandle = (content) => { //支持粘贴
            return content
        }
        this.editor.create()
      }
  }
</script>
<style scoped>
    #editorElem /deep/ .w-e-text-container{ //设置富文本高度,富文本有个默认300px的高度
        height:400px !important;
    } 
</style>

效果图:

对于几种知名的富文本对比以及优缺点,可以参考:

几种知名开源富文本编辑器记录和对比

富文本封装成子组件

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值