vue使用wangEditor创建富文本

安装

javascript 复制代码
npm install @wangeditor/editor --save
npm install @wangeditor/editor-for-vue --save
npm install @wangeditor/plugin-upload-attachment//附件使用

vue

图片上传、视频上传、附件上传

javascript 复制代码
<template lang="html">
    <div style="border: 1px solid #ccc;width:840px" class="editors">
        <Toolbar style="border-bottom: 1px solid #ccc" :editor="editor" :defaultConfig="toolbarConfig" :mode="mode" />
        <Editor style="height: 400px; overflow-y: hidden;" v-model="html" @input="handleInput"
            :defaultConfig="editorConfig" :mode="mode" @onCreated="onCreated" @onChange="onChange"
            @onMaxLength="onMaxLength" />
    </div>
</template>
<script>
import Vue from "vue";
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
import { DomEditor } from "@wangeditor/editor";
import "@wangeditor/editor/dist/css/style.css";
import { getToken } from "@/utils/auth";
export default Vue.extend({
  components: { Editor, Toolbar },
  model: {
    prop: "value", // v-model 接收的值=props的message属性接收的值
    event: "change", // v-model 发生变化时触发的方法
  },
  props: {
    value: {
      // 富文本数据
      type: String,
      default: "",
    },
    id: {
      type: String,
      default: "",
    },
    maxLength: {
      type: Number,
      default: 10000000,
    },
  },
  watch: {
    value: function (value) {
      // 如果值不相等再进行赋值,避免多次赋值造成闪烁
      if (value !== this.html) {
        this.html = this.value;
      }
    },
    // value为编辑框输入的内容,这里我监听了一下值,当父组件调用得时候,如果给value赋值了,子组件将会显示父组件赋给的值
  },
  data() {
    return {
      editor: null,
      html: "",
      textLength: 0,
      // 编辑器模式
      mode: "default", // or 'simple'
      toolbarConfig: {
        // 排除工具栏配置
        excludeKeys: [
          // 全屏,网络视频,本地视频,表情,代码,待办,代码块,插入图片
          "fullScreen",
          // "uploadVideo",
          // "group-video",
          // "emotion",
          // "code",
          // "todo",
          // "codeBlock",
          "insertImage",
        ],
        insertKeys: {
          index: 22, // 自定义插件在工具栏显示的位置
          keys: ["uploadAttachment"], // 查看名称
        },
        hoverbarKeys: {
          attachment: {
            menuKeys: ["downloadAttachment"], // "下载附件"菜单
          },
        },
      },
      italicButton: null,
      editorConfig: {
        placeholder: "请输入内容...",
        maxLength: this.maxLength, // 限制富文本输入文本字数
        MENU_CONF: {
          uploadImage: {
            server: process.env.VUE_APP_BASE_API + "/file",
            timeout: 100000,
            maxFileSize: 10000 * 1024 * 1024, 
            base64LimitSize: 50000 * 1024,
          },
          uploadVideo: {
            fieldName: "file",
            server: process.env.VUE_APP_BASE_API + "/file",
            timeout: 1000000,
            maxFileSize: 10000 * 1024 * 1024, // 1M
            base64LimitSize: 50000 * 1024,
            headers: {
              Authorization: getToken(),
            },
            async customInsert(insertFn, result) {
              if (insertFn.code === 0) {
                result(insertFn.data[0]);
              } else {
                this.$message.error("视频上传失败,请重新上传!");
              }
            },
          },
          uploadAttachment: {
            fieldName: "file",
            server: process.env.VUE_APP_BASE_API + "/file",
            timeout: 1000000,
            headers: {
              Authorization: getToken(),
            },
            async customInsert(res, file,insertFn) {
              insertFn(
                file.name,
                res.data[0]
              );
            },
          },
          fontFamily: {
            fontFamilyList: [
              { name: "仿宋", value: "fangsong" },
              { name: "黑体", value: "Arial" },
              { name: "仿手写", value: "cursive" },
              "serif",
              "sans-serif",
            ],
          },
        },
      },
      // Toolbar对象
      curToolbarConfig: null,
    };
  },

  methods: {
    onCreated(editor) {
      this.editor = Object.seal(editor); // 一定要用 Object.seal() ,否则会报错
      this.$nextTick(() => {
        const toolbar = DomEditor.getToolbar(this.editor);
        this.curToolbarConfig = toolbar.getConfig();
      });
    },
    handleInput(value) {
      this.$emit("input", value); // 将内容同步到父组件中
    },
    onChange(editor) {
      // 监听富文本输入
      const text = editor.getText().replace(/\n|\r/gm, "");
      this.textLength = text.length;
    },
    onMaxLength(editor) {
      // 当输入值达到限制值时触发事件
      this.$message.warning("输入文本数已到达最大值!");
    },
  },
  mounted() {},
  beforeDestroy() {
    const editor = this.editor;
    if (editor == null) return;
    editor.destroy(); // 组件销毁时,及时销毁编辑器
  },
});
</script>
<style lang="scss" scoped>
::v-deep .w-e-textarea-video-container {
  background-image: none;
}
</style>

main.js

javascript 复制代码
import {Boot} from '@wangeditor/editor'
import attachmentModule from '@wangeditor/plugin-upload-attachment'
Boot.registerModule(attachmentModule)
相关推荐
「、皓子~25 分钟前
后台管理系统的诞生 - 利用AI 1天完成整个后台管理系统的微服务后端+前端
前端·人工智能·微服务·小程序·go·ai编程·ai写作
就改了27 分钟前
Ajax——在OA系统提升性能的局部刷新
前端·javascript·ajax
凌冰_29 分钟前
Ajax 入门
前端·javascript·ajax
京东零售技术44 分钟前
京东小程序JS API仓颉改造实践
前端
奋飛1 小时前
TypeScript系列:第六篇 - 编写高质量的TS类型
javascript·typescript·ts·declare·.d.ts
老A技术联盟1 小时前
从小白入门,基于Cursor开发一个前端小程序之Cursor 编程实践与案例分析
前端·小程序
风铃喵游1 小时前
构建引擎: 打造小程序编译器
前端·小程序·架构
sunbyte1 小时前
50天50个小项目 (Vue3 + Tailwindcss V4) ✨ | ThemeClock(主题时钟)
前端·javascript·css·vue.js·前端框架·tailwindcss
小飞悟1 小时前
🎯 什么是模块化?CommonJS 和 ES6 Modules 到底有什么区别?小白也能看懂
前端·javascript·设计
浏览器API调用工程师_Taylor1 小时前
AOP魔法:一招实现登录弹窗的全局拦截与动态处理
前端·javascript·vue.js