【vue教程】wangeditor富文本编辑器使用

一、文档

官网:www.wangEditor.com
文档:www.kancloud.cn/wangfupeng/wangeditor3/332599
源码:github.com/wangfupeng1988/wangEditor

二、下载

npm install wangeditor

三、使用

import E from "wangeditor";
const editor = new E("#div1");
editor.create();

四、在vue中使用

①新建一个组件

<template>
  <div class="editor">
    <div id="e">
    </div>
  </div>
</template>

<script>
  import Vue from 'vue'
  import E from "wangeditor"
  export default {
    name: 'editor',
    data() {
      return {
        content: "",
        editor: null,
        info_: null
      }
    },
    model: {
      prop: 'desc',
      event: 'change'
    },
    watch: {
      isClear(val) {
        // console.log(val)
        if (val) {
          this.editor.txt.clear()
          // this.info_=null
        }
      },
      desc(value) {
        //console.log("desc",value)
        if (value != this.editor.txt.html()) {
          this.editor.txt.html(this.desc)
        }
      }
    },
    props: {
      desc: {
        type: String,
        default: ""
      },
      //业务中我们经常会有添加操作和编辑操作,添加操作时,我们需清除上一操作留下的缓存
      isClear: {
        type: Boolean,
        default: false
      }
    },

    methods: {
      initE() {
        this.editor = new E('#e')
        console.log('this.editorthis.editor', this.editor)
        this.editor.config.onchangeTimeout = 1000 // 单位 ms
        this.editor.config.height = 500
        this.editor.config.uploadFileName = 'file'
        this.editor.config.uploadImgServer = `` // 你的服务器地址
        this.editor.config.uploadImgHooks = {
          before: function(xhr, editor, files) {
            // 图片上传之前触发
            // xhr 是 XMLHttpRequst 对象,editor 是编辑器对象,files 是选择的图片文件

            // 如果返回的结果是 {prevent: true, msg: 'xxxx'} 则表示用户放弃上传
            // return {
            //     prevent: true,
            //     msg: '放弃上传'
            // }
          },
          success: function(xhr, editor, result) {
            // 图片上传并返回结果,图片插入成功之后触发
            // xhr 是 XMLHttpRequst 对象,editor 是编辑器对象,result 是服务器端返回的结果
          },
          fail: function(xhr, editor, result) {
            // 图片上传并返回结果,但图片插入错误时触发
            // xhr 是 XMLHttpRequst 对象,editor 是编辑器对象,result 是服务器端返回的结果
          },
          error: function(xhr, editor) {
            // 图片上传出错时触发
            // xhr 是 XMLHttpRequst 对象,editor 是编辑器对象
          },
          timeout: function(xhr, editor) {
            // 图片上传超时时触发
            // xhr 是 XMLHttpRequst 对象,editor 是编辑器对象
          },

          // 如果服务器端返回的不是 {errno:0, data: [...]} 这种格式,可使用该配置
          // (但是,服务器端返回的必须是一个 JSON 格式字符串!!!否则会报错)
          customInsert: function(insertImg, result, editor) {
            // 图片上传并返回结果,自定义插入图片的事件(而不是编辑器自动插入图片!!!)
            // insertImg 是插入图片的函数,editor 是编辑器对象,result 是服务器端返回的结果

            // 举例:假如上传图片成功后,服务器端返回的是 {url:'....'} 这种格式,即可这样插入图片:
            console.log(result.data)
            var url = result.data
            insertImg(url)

            // result 必须是一个 JSON 格式字符串!!!否则报错
          }
        }
        this.editor.config.onchange = (html) => {
          this.info_ = html // 绑定当前逐渐地值
          this.$emit('change', this.info_) // 将内容同步到父组件中
        }
        this.editor.config.menus = [
          'head',
          'bold',
          'fontSize',
          'fontName',
          'italic',
          'underline',
          'strikeThrough',
          'indent',
          'lineHeight',
          'foreColor',
          'backColor',
          'link',
          'list',
          'todo',
          'justify',
          'quote',
          'emoticon',
          'image',
          'video',
          'table',
          'code',
          'splitLine',
          'undo',
          'redo',
        ]
        this.editor.create()
        // this.editor.txt.html(this.desc)
        //  this.editor.txt.html(this.desc)
      }
    },
    mounted() {
      this.initE();
    }
  }
</script>

<style scoped>

</style>

②在相应页面使用该组件

<editor v-model="desc" :isClear="isClear"></editor>
import editor from '@/你文件放的位置/editor.vue'
export default{
data(){
	 isClear: false, //清除富文本编辑器内容,
	 desc:''
},
components:{
	editor
}
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值