VUE富文本编辑器wangeditor

网站地址:https://www.wangeditor.com/

npm安装:npm i wangeditor --save
封装:

<template>
  <div>
    <div
      ref="editor"
    ></div>
  </div>
</template>

<script>
  import WangEditor from "wangeditor"
  export default {
    name: "WangEditor",

    data() {
      return {
        token: "",
        editorE:""//编辑器的对象
      }
    },
    //接收v-model的值
    props: {
      value: String
    },
    model: {
      prop: 'value',
      event: 'change'
    },
    mounted() {
      //初始化编辑框
      this.init();
      this.editorE.txt.html(this.value)
    },
    created(){
      this.token = localStorage.getItem('t_k')
    },

    watch:{
      value(val){//普通的watch监听
        if (val !== this.editorE.txt.html()) {
          this.editorE.txt.html(val)
        }
      }
    },

    methods: {
      init() {
        const _this = this;
        /*实例化*/
        _this.editorE = new WangEditor(_this.$refs.editor);
      // 配置 server 接口地址
        _this.editorE.config.uploadImgServer = process.env.VUE_APP_API_HOST+'/api/v1/file/uploadImage'
        //设置富文本的高度
        _this.editorE.config.height = 600
        //默认限制图片大小是 5M
        _this.editorE.config.uploadImgMaxSize = 8 * 1024 * 1024 // 8M
        //自定义上传的参数
        _this.editorE.config.uploadFileName = 'file'
        //自定义头部token信息
        _this.editorE.config.uploadImgHeaders = {
          "token":_this.token
        }
        // //设置请求参数
        // _this.editorE.config.uploadImgParams = {
        //   suffixPath: '@nowm' //后缀水印
        // }
        //timeout 即上传接口等待的最大时间,默认是 10 秒钟,可以自己修改
        _this.editorE.config.uploadImgTimeout = 3 * 1000


        _this.editorE.config.uploadImgHooks = {
          // 上传图片之前
          before: function(xhr) {

          /*  // 可阻止图片上传
            return {
              prevent: true,
              msg: '需要提示给用户的错误信息'
            }*/
          },
          // 图片上传并返回了结果,图片插入已成功
          success: function(xhr) {
            //console.log('success', xhr)
          },
          // 图片上传并返回了结果,但图片插入时出错了
          fail: function(xhr, editor, resData) {
          },
          // 上传图片出错,一般为 http 请求的错误
          error: function(xhr, editor, resData) {
          },
          // 上传图片超时
          timeout: function(xhr) {
          },
          // 图片上传并返回了结果,想要自己把图片插入到编辑器中
          // 例如服务器端返回的不是 { errno: 0, data: [...] } 这种格式,可使用 customInsert
          customInsert: function(insertImgFn, result) {
            // result 即服务端返回的接口
            // insertImgFn 可把图片插入到编辑器,传入图片 src ,执行函数即可
            insertImgFn(result.data.src)
          }
        }

        /*创建编辑器*/
        _this.editorE.create();
        // 禁用
       // _this.editorE.disable();
      // 监控变化,同步更新到文本 向上传递更新
        _this.editorE.config.onchange = function (html) {
          _this.$emit("change", html);
        }


      }
    }

  };
</script>
<style  lang="less" scoped>
  .home {
    width: 100%;
    margin: auto;
    position: relative;
    z-index: -1;
  }
   /deep/.w-e-text{
    text-align: left !important;
  }
</style>

使用方法

import EditorBar from './fuwenben.vue'
components: { EditorBar },
<div>
       <editor-bar v-model="editFrom.content"></editor-bar>
</div>

注意:有时候粗体与斜体可能会没有效果,
解决方法:进入assets/css/reset.css文件夹将样式注释,如下图
在这里插入图片描述
隐藏toolbar样式

  /deep/.w-e-text{
    border-top: 1px solid #ccc;
    text-align: left !important;
  }
  /deep/.w-e-toolbar{
    display: none;
  }

富文本禁用

// 禁用
      _this.editorE.disable();
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值