wangeditor 富文本编辑器 在vue2.0 项目中安装与使用(复制可用)

1、安装npm install wangeditor

2、封装组件

<template lang="html">
  <div class="editor">
    <div ref="toolbar" class="toolbar"></div>
    <div ref="editor" class="text"></div>
  </div>
</template>
<script>
import E from 'wangeditor'
export default {
  name: 'editoritem',
  data() {
    return {
      editor: null,
      info_: null
    }
  },
  model: {
    prop: 'value',
    event: 'change'
  },
  props: {
    value: {
      type: String,
      default: ''
    },
    disable: {
      type: null,
      default: null
    },
    isClear: {
      type: Boolean,
      default: false
    }
  },
  watch: {
    isClear(val) {
      if (val) {
        this.editor.txt.clear()
        this.info_ = null
      }
    },
    value: function(value) {
      if (value !== this.editor.txt.html()) {
        this.editor.txt.html(this.value)
      }
    }
  },
  mounted() {
    this.seteditor()
    this.editor.txt.html(this.value)
  },
  methods: {
    seteditor() {
      this.editor = new E(this.$refs.toolbar, this.$refs.editor)
      this.editor.customConfig = this.editor.customConfig
        ? this.editor.customConfig
        : this.editor.config
      this.editor.customConfig.uploadImgShowBase64 = false // base 64 存储图片
      this.editor.customConfig.uploadImgHeaders = {} // 自定义 header
      this.editor.customConfig.uploadFileName = 'file' // 后端接受上传文件的参数名
      this.editor.customConfig.uploadImgMaxSize = 2 * 1024 * 1024 // 将图片大小限制为 2M
      this.editor.customConfig.uploadImgMaxLength = 10 // 限制一次最多上传 10 张图片
      this.editor.customConfig.uploadImgTimeout = 5 * 60 * 1000 // 设置超时时间
      this.editor.customConfig.customUploadImg = async (files, insertImgFn) => {
         //自定义上传功能,注释部分是举例说明
        // for (let file of files) {
        //   let formdata = new FormData()
        //   formdata.append('files', file)
        //   var obj = {}
        //   obj.createPersonId = this.$userInfo.EmployeeID
        //   formdata.append(
        //     'fileRecordDto',
        //     new Blob([JSON.stringify(obj)], { type: 'application/json' })
        //   )
        //   this.$uplod.post('file/upload', formdata).then(res => {
        //     for (let index = 0; index < res.data.length; index++) {
        //       const element = res.data[index]
        //       insertImgFn(element.fileUrl) //上传成功后把图片插入编辑器里
        //     }
        //   })
        // }
      }
      // 配置菜单
      this.editor.customConfig.menus = [
        'undo', // 撤销
        'redo', // 重复
        'head', // 标题
        'bold', // 粗体
        'fontSize', // 字号
        'fontName', // 字体
        'italic', // 斜体
        'underline', // 下划线
        'strikeThrough', // 删除线
        'foreColor', // 文字颜色
        'backColor', // 背景颜色
        'link', // 插入链接
        'list', // 列表
        'justify', // 对齐方式
        'quote', // 引用
        'emoticon', // 表情
        'image', // 插入图片
        'table', // 表格
        'code' // 插入代码
      ]

      this.editor.customConfig.onchange = html => {
        this.info_ = html 
        if (!this.disable) {
          this.$emit('change', this.info_) // 将内容同步到父组件中
        }
      }
      this.editor.create() // 创建富文本编辑器
      if (this.disable) {
        this.editor.disable() //只读
      } else {
        this.editor.enable()
      }
    }
  }
}
</script>
<style lang="css">
.editor {
  width: 100%;
  margin: 0 auto;
  position: relative;
  z-index: 0;
}
.toolbar {
  border: 1px solid #ccc;
  display: flex;
  flex-wrap: wrap;
}
.text {
  border: 1px solid #ccc;
  border-top: none;
  min-height: 500px;
}
</style>

 3、使用方法

 <wang-editor v-model="this.Content" :disable="1" @change="change"></wang-editor> //使用

import WangEditor from '../../components/WangEditor'   //引入组件
export default {
  components: {
    WangEditor//挂载
  },
  data() {
    return {}
   }
}

效果展示

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值