前端使用富文本 组件化 可直接使用

富文本组件

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

<script>
import E from "wangeditor";
export default {
  data() {
    return {
      editor: "",
      content:"",
      info_:null
    };
  },
  model: {
    prop: 'desc',
    event:'change'
  },
  // created(){
  //   this.getHtml();
  //   this.setHtml();
  // },
  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: {
    init() {
      const _this = this;
      this.$nextTick(()=>{
        this.editor = new E('#editor')
        //这里各位小伙伴可以查询官网,根据自己的需求进行菜单栏调整
        this.setMenus();
        this.setImg();
        this.editor.config.onchange = (html) => {
          this.info_ = html // 绑定当前逐渐地值
          this.$emit('change', this.info_) // 将内容同步到父组件中
        }
        this.editor.create()
      })

    },
    setMenus() {
      this. editor.config.menus = [
        'head',  // 标题
        'bold',  // 粗体
        'fontSize',  // 字号
        'fontName',  // 字体
        'italic',  // 斜体
        'underline',  // 下划线
        'strikeThrough',  // 删除线
        'foreColor',  // 文字颜色
        'backColor',  // 背景颜色
        'link',  // 插入链接
        'list',  // 列表
        'justify',  // 对齐方式
        'quote',  // 引用
        'emoticon',  // 表情
        'image',  // 插入图片
        'table',  // 表格
        'code',  // 插入代码
        'undo',  // 撤销
        'redo'  // 重复
      ]

    },
    setImg(){
      this.editor.config.onchangeTimeout = 1000 // 单位 ms
      this.editor.config.uploadFileName = 'file'
      this.editor.config.uploadImgServer = `url`  // 你的服务器地址
      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.url)
          var url = result.url
          insertImg(url)

          // result 必须是一个 JSON 格式字符串!!!否则报错
        }
      }
    },
    // getHtml(txt) {
    //    this.editor.txt.html(txt);
    //   // this.editor.txt.html();
    //   console.log(  this.editor.txt.html(txt))
    // },
    // setHtml(txt) {
    //   this.editor.txt.html(txt);
    //   console.log(this.editor.txt.html(txt))
    // }
  },
  mounted() {
    this.$nextTick(function() {
      this.init();
    });
  }
};
</script>

使用

import editor @/compunent/editor.vue
components: {
    editor
  },
<editor ref="editor"  v-model="form.contents" :isClear="isClear">
 </editor>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

38?

感觉写的有帮助可以多多关注

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值