vue 富文本编辑器 Editor 使用

	NPM版本
    "wangeditor": "^4.7.4"

isClear2默认false 监听触发清空组件内的富文本编辑器
一般是关闭弹框之类的使用
change也没用上 根据需求来吧

页面注册引用
<EditorThree ref="editorTwo"
                         v-model="formInfo.data.levelInfo"
                         :isClear2="isClear2"
                         @change="change2">
 </EditorThree>

下面为富文本编辑器封装的单组件

<template lang="html">  
    <div ref="editor" class="text">  
  </div>  
</template>  
  
<script>
import E from "wangeditor";
// import "wangeditor/release/wangEditor.min.css";
export default {
  name: "editoritem",
  data () {
    return {
      // uploadPath,
      editor: null,
      info_: null
    };
  },
  model: {
    prop: "value",
    event: "change2"
  },
  props: {
    value: {
      type: String,
      default: ""
    },
    isClear2: {
      type: Boolean,
      default: false
    }
  },
  watch: {
    isClear2 (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);
      }
    }
    //value为编辑框输入的内容,这里我监听了一下值,当父组件调用得时候,如果给value赋值了,子组件将会显示父组件赋给的值
  },
  mounted () {
    this.seteditor();
    this.editor.txt.html(this.value);
  },
  methods: {
    seteditor () {
      //   this.editor = new E(this.$refs.toolbar, this.$refs.editor);
      this.editor = new E(this.$refs.editor);
      // 配置菜单
      this.editor.config.menus = [
        // "head", // 标题
        "bold", // 粗体
        "fontSize", // 字号
        "fontName", // 字体
        "italic", // 斜体
        "underline", // 下划线
        "strikeThrough", // 删除线
        "foreColor", // 文字颜色
        "link", // 插入链接
        "list", // 列表
        "justify", // 对齐方式
        "quote", // 引用
        // 'image', // 插入图片
        "location" // 位置
      ];

      this.editor.config.onchange = html => {
        this.info_ = html; // 绑定当前逐渐地值
        this.$emit("change2", this.info_); // 将内容同步到父组件中
      };

      // 设置高度
      this.editor.config.height = 200;
      // 创建富文本编辑器
      this.editor.create();
    }
  }
};
</script>  
  
<style lang="css" scoped>
</style>  
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值