vue---富文本编辑器插件的使用---wangeditor

wangeditor 是一个轻量级 web 富文本编辑器,配置方便,使用简单。
官网:wangeditor

实现效果

在这里插入图片描述

获取到的数据格式

在这里插入图片描述

具体使用

安装依赖
npm install wangeditor
创建公用组件:在src/components/WangEditor文件夹中创建index.vue
<template lang="html">
  <div class="wangeditor">
      <div ref="toolbar" class="toolbar"></div>
      <div ref="wangeditor" class="text"></div>
  </div>
</template>
 
<script>
import E from "wangeditor";
export default {
  data() {
    return {
      wangEditor: null,
      wangEditorInfo: null
    };
  },
  model: {
    prop: "value",
    event: "change"
  },
  props: {
    value: {
      type: String,
      default: ""
    },
    isClear: {
      type: Boolean,
      default: false
    }
  },
  watch: {
    isClear(val) {
      // 触发清除文本域内容
      if (val) {
        this.wangEditor.txt.clear();
        this.wangEditorInfo = null;
      }
    },
    value: function(value) {
      if (value !== this.wangEditor.txt.html()) {
        this.isClear = false;
        this.wangEditor.txt.html(this.value); //value为编辑框输入的内容,这里我监听了一下值,当父组件调用得时候,如果给value赋值了,子组件将会显示父组件赋给的值
      }
    }
  },
  mounted() {
    this.initEditor();
    this.wangEditor.txt.html(this.value);
  },
  methods: {
    initEditor() {
      this.wangEditor = new E(this.$refs.toolbar, this.$refs.wangeditor);
      this.wangEditor.customConfig = this.wangEditor.customConfig ? this.wangEditor.customConfig : this.wangEditor.config
      this.wangEditor.customConfig.uploadImgShowBase64 = true; // base64存储图片(推荐)
      //this.wangEditor.customConfig.uploadImgServer = "https://jsonplaceholder.typicode.com/posts/"; // 配置服务器端地址(不推荐)
      this.wangEditor.customConfig.uploadImgHeaders = {}; // 自定义header
      this.wangEditor.customConfig.uploadFileName = "file"; // 后端接受上传文件的参数名
      this.wangEditor.customConfig.uploadImgMaxSize = 2 * 1024 * 1024; // 将图片大小限制为(默认最大支持2M)
      this.wangEditor.customConfig.uploadImgMaxLength = 6; // 限制一次最多上传6张图片
      this.wangEditor.customConfig.uploadImgTimeout = 1 * 60 * 1000; // 设置超时时间(默认1分钟)
 
      // 配置菜单
      this.wangEditor.customConfig.menus = [
        "head", // 标题
        "bold", // 粗体
        "fontSize", // 字号
        "fontName", // 字体
        "italic", // 斜体
        "underline", // 下划线
        "strikeThrough", // 删除线
        "foreColor", // 文字颜色
        "backColor", // 背景颜色
        "link", // 插入链接
        "list", // 列表
        "justify", // 对齐方式
        "quote", // 引用
        "emoticon", // 表情
        "image", // 插入图片
        "table", // 表格
        "video", // 插入视频
        "code", // 插入代码
        "undo", // 撤销
        "redo", // 重复
        "fullscreen" // 全屏
      ];
      this.wangEditor.customConfig.uploadImgHooks = {
        fail: (xhr, editor, result) => {
          // 插入图片失败回调
        },
        success: (xhr, editor, result) => {
          // 图片上传成功回调
        },
        timeout: (xhr, editor) => {
          // 网络超时的回调
        },
        error: (xhr, editor) => {
          // 图片上传错误的回调
        },
        customInsert: (insertImg, result, editor) => {
          // 图片上传成功,插入图片的回调(不推荐)
          insertImg(result.url);
        }
      };
      this.wangEditor.customConfig.onchange = html => {
        this.wangEditorInfo = html;
        this.$emit("change", this.wangEditorInfo); // 将内容同步到父组件中
      };
      // 创建富文本编辑器
      this.wangEditor.create();
    }
  }
};
</script>
<style lang="less">
.wangeditor {
  border: 1px solid #e6e6e6;
  box-sizing: border-box;
  .toolbar {
    border-bottom: 1px solid #e6e6e6;
    box-sizing: border-box;
  }
  .text {
    min-height: 300px;
  }
}
</style>
在项目中使用
index.js中引用
import WangEditor from "@/components/WangEditor/index"
export default {
    components: { WangEditor }data: function() {
    	return {
    		isClear: false,//设置为true的时候,这个可以用this.wangEditorDetail=''来替代
            wangEditorDetail: ""
    	}
    },
    mounted() {
        this.wangEditorDetail = "我是默认值"; //设置富文本框默认显示内容
    },
    methods: {
         wangEditorChange(val) {
            console.log(“我是富文本的内容”+val);
            }}
index.vue中
 <WangEditor v-model="wangEditorDetail" :isClear="isClear" @change="wangEditorChange"></WangEditor>
可能问题

报错 Cannot set property ‘uploadImgShowBase64‘ of undefined“
在这里插入图片描述

问题原因

因npm i直接安装为最新版本,而wangeditor更新了版本老的customConfig 变成了config,可以把this.wangEditor.customConfig 全部替换成 this.wangEditor.config就能正常运行了;
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
wangeditor 富文本编辑器支持代码块的功能。通过使用 wangeditor 的代码块插件,你可以在编辑器中插入代码块,并进行代码高亮显示。下面是一个示例代码,展示如何在 wangeditor使用代码块功能: 1. 首先,确保已经引入了 wangeditor 的相关文件和依赖。 2. 在 Vue 组件中,添加一个 textarea 元素作为编辑器的容器: ```html <textarea id="editor" v-model="content"></textarea> ``` 3. 在 Vue 组件的 `mounted` 钩子函数中,初始化 wangeditor 编辑器并配置代码块插件: ```javascript import Editor from 'wangeditor' export default { mounted() { const editor = new Editor('#editor') editor.config.plugins = [ // 其他插件... CodeSyntaxHighlighting() // 代码块插件 ] editor.create() // 监听编辑内容变化 editor.txt.on('change', () => { this.content = editor.txt.html() }) }, data() { return { content: '' } } } ``` 4. 在样式文件中,引入代码块的 CSS 文件: ```css @import "~wangeditor/dist/css/wangEditor-codeHighlight.css"; ``` 这样就可以在 wangeditor 编辑器中使用代码块功能了。你可以输入代码,并选择对应的编程语言,然后点击插入代码块按钮即可实现代码高亮显示。 请注意,上述示例中的代码只是一个简单的示例,具体的使用方法和配置可能会因为你的项目和需求而有所不同。你可以根据 wangeditor 的官方文档和示例代码进行更详细的配置和使用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

hyduan200

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

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

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

打赏作者

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

抵扣说明:

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

余额充值