vue弹出框中使用wangeditor富文本(新手教程)

1,第一步,当然是先下载依赖

	npm i wangeditor --save

2,第二步,在vue单页面中引用

<script>
	import E from "wangeditor";
</script>

3,第三步,写一个弹窗框

<template>
  <div>
    <el-button type="success" @click="addfrom">添加</el-button>

    <el-dialog
      :title="eldialog"
      :visible.sync="dialogVisible"
      @opened="eldialingShow"
      width="50%"
      :close-on-click-modal="false"
    >
      <div id="content"></div>
    </el-dialog>
  </div>
</template>

<script>
import E from "wangeditor";
export default {
  data() {
    return {
      eldialog: "",
      text: "",
      dialogVisible: false,
    };
  },
  created() {},
  methods: {
      // 打开弹出框
      addfrom() {
      // 清空之前的富文本内容
      this.text = "";
      // 更改弹出框的标题
      this.eldialog = "编辑富文本";
      // 显示富文本
      this.dialogVisible = true;
    },
    // 弹出框显现后的回调
    eldialingShow() {
      this.$nextTick(() => {
       
      });
    },
  },
};
</script>

至此,准备工作已完成,实际上也没剩什么了
接下来就是调用富文本的函数
随便点,起个fun1吧

 // 富文本的事件
    fun1() {
      this.showif = true;
    // 先判断富文本是添加还是修改,
    // 如果this.text为空则是添加富文本内容,若是修改富文本的内容,只需要将之前的富文本内容赋给this.text即可
      if (this.text == "") {
        let that = this;
        const editor = new E(document.getElementById("content"));
        editor.config.onchange = function (newHtml) {
         // that.editorContent即为最新的富文本内容
          that.editorContent = newHtml;
        };
        editor.create();
        editor.txt.html(this.text);
      } else {
        this.textare = this.text;
        let that = this;
        const editor = new E(document.getElementById("content"));
        this.textare.replace(/&lt;/g, "<").replace(/&gt;/g, ">");
        editor.config.onchange = function (newHtml) {
            
            // that.editorContent即为最新的富文本内容
          that.editorContent = newHtml;
        };
        editor.create();
        this.textare = this.escape2Html(this.textare);
        editor.txt.html(this.textare);
      }
    },
    escape2Html(str) {
      str = str
        .replace(/&lt;/g, "<")
        .replace(/&gt;/g, ">")
        .replace(/&amp;/g, "&")
        .replace(/&quot;/g, '"')
        .replace(/&#39;/g, "'")
        .replace(/&npsp;/g, " ")
        .replace(/\n/g, "<br />")
        .replace(/\r/g, "<br />");
      return str;
    },

至此,附上所有代码

<template>
  <div>
    <el-button type="success" @click="addfrom">添加</el-button>

    <el-dialog
      :title="eldialog"
      :visible.sync="dialogVisible"
      @opened="eldialingShow"
      width="50%"
      :close-on-click-modal="false"
    >
      <div id="content"></div>
    </el-dialog>
  </div>
</template>


<script>
import E from "wangeditor";
export default {
  data() {
    return {
      eldialog: "",
      text: "",
      dialogVisible: false,
    };
  },
  created() {},
  methods: {
      // 打开弹出框
      addfrom() {
      // 清空之前的富文本内容
      this.text = "";
      // 更改弹出框的标题
      this.eldialog = "编辑富文本";
      // 显示富文本
      this.dialogVisible = true;
    },
    // 弹出框显现后的回调
    eldialingShow() {
      this.$nextTick(() => {
        this.fun1();
      });
    },
    // 富文本的事件
    fun1() {
      this.showif = true;
    // 先判断富文本是添加还是修改,
    // 如果this.text为空则是添加富文本内容,若是修改富文本的内容,只需要将之前的富文本内容赋给this.text即可
      if (this.text == "") {
        let that = this;
        const editor = new E(document.getElementById("content"));
        editor.config.onchange = function (newHtml) {
         // that.editorContent即为最新的富文本内容
          that.editorContent = newHtml;
        };
        editor.create();
        editor.txt.html(this.text);
      } else {
        this.textare = this.text;
        let that = this;
        const editor = new E(document.getElementById("content"));
        this.textare.replace(/&lt;/g, "<").replace(/&gt;/g, ">");
        editor.config.onchange = function (newHtml) {
            
            // that.editorContent即为最新的富文本内容
            
          that.editorContent = newHtml;
        };
        editor.create();
        this.textare = this.escape2Html(this.textare);
        editor.txt.html(this.textare);
      }
    },
    escape2Html(str) {
      str = str
        .replace(/&lt;/g, "<")
        .replace(/&gt;/g, ">")
        .replace(/&amp;/g, "&")
        .replace(/&quot;/g, '"')
        .replace(/&#39;/g, "'")
        .replace(/&npsp;/g, " ")
        .replace(/\n/g, "<br />")
        .replace(/\r/g, "<br />");
      return str;
    },
  },
};
</script>

<style lang="scss" scoped>
</style>

重要!!!! this.editorContent 即为自己编辑的富文本内容

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

武良神

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值