前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >wangEditor富文本编辑

wangEditor富文本编辑

作者头像
明知山
发布于 2022-05-05 12:11:05
发布于 2022-05-05 12:11:05
1.7K00
代码可运行
举报
文章被收录于专栏:前端开发随笔前端开发随笔
运行总次数:0
代码可运行

安装

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
npm install wangEditor -s

页面引入

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
import wangEditor from "wangeditor";

完整代码

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<template>
  <div class="box">
    <div id="editor"></div>
    <div class="btn">
      <el-button type="primary" @click="getEditorData"
        >生成的html代码</el-button
      >
    </div>
    <h3>内容预览</h3>
    <div class="preview" v-html="editorData"></div>
  </div>
</template>

<script>
import wangEditor from "wangeditor";
export default {
  data() {
    return {
      editor: null,
      editorData: "",
    };
  },
  mounted() {
    this.initEditor();
  },

  beforeDestroy() {
    // 调用销毁 API 对当前编辑器实例进行销毁
    this.editor.destroy();
    this.editor = null;
  },

  methods: {
    // 初始化插件
    initEditor() {
      const editor = new wangEditor("#editor");
      editor.config.placeholder = "请输入内容";
      editor.config.onchangeTimeout = 0;
      editor.config.uploadImgShowBase64 = true;
      // 配置 onchange 回调函数,将数据同步到 vue 中
      editor.config.onchange = (res) => {
        this.editorData = res;
      };
      // 创建编辑器
      editor.create();
      this.editor = editor;
    },

    // 获取编辑器内容
    getEditorData() {
      let data = this.editor.txt.html();
      this.$notify({
        title: "html代码",
        message: data,
        position: "bottom-left",
        duration: 0,
      });
    },
  },
};
</script>

<style lang="less" scoped>
.box {
  width: 80%;
  margin: 20px auto;
}

.btn {
  display: flex;
  flex-direction: row-reverse;
}

.preview {
  width: 100%;
  min-height: 300px;
  margin: 20px auto 0;
  border: 1px solid #ccc;
}

.el-notification.left {
  word-break: break-all;
}
</style>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-03-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档