vue3中使用WangEditor 富文本编辑器

附官网:https://www.wangeditor.com/

1.安装 + 引入

npm install @wangeditor/editor --save

// 组件中

import "@wangeditor/editor/dist/css/style.css"; // 引入 css

import { Editor, Toolbar } from "@wangeditor/editor-for-vue";

2.组件template模板中 

 <div style="border: 1px solid #ccc">
          <Toolbar
            style="border-bottom: 1px solid #ccc"
            :editor="editorRef"
            :defaultConfig="toolbarConfig"
          />
          <Editor
            style="height: 300px; overflow-y: hidden"
            v-model="newsParameterList.news_content"
            @onCreated="handleCreated"
            :defaultConfig="editorConfig"
          />
</div>

// newsParameterList.news_content  为富文本内容绑定

3.具体配置

import {  shallowRef } from "vue";
//编辑器实例,必须用 shallowRef
let editorRef = shallowRef();
let handleCreated = (editor: any) => {
  editorRef.value = editor; // 记录 editor 实例,重要!
};

// 菜单配置项    删除没有太大用途的菜单项
let toolbarConfig = {
  excludeKeys: [
    "insertVideo", // 删除视频
    "uploadVideo",
    "group-video",
    "insertImage", // 删除网络图片上传
    "insertLink", // 删除链接
    "insertTable", // 删除表格
    "codeBlock", // 删除代码块
    "blockquote",
    "todo",
    "emotion",
    "fontSize",
    "fontFamily",
    "lineHeight",
    "group-more-style",
    "color",
    "bgColor",
  ],
};

//编辑器实例,必须用 shallowRef
let editorRef = shallowRef();
let handleCreated = (editor: any) => {
  editorRef.value = editor; // 记录 editor 实例,重要!
};

const editorConfig = {
  MENU_CONF: {
    uploadImage: {
      server: "/api/upload",
      allowedFileTypes: ["image/*"],
      fieldName: "your-fileName",
      base64LimitSize: 3 * 1024 * 1024,
      // 单个文件的最大体积限制,为 3M
      maxFileSize: 3 * 1024 * 1024,
      maxNumberOfFiles: 1,
      onError(file: any) {
        proxy.$message.warning("当前选择的图片过大!请选择3M以内");
      },
    },
  },
};

4.编辑回显时

//编辑器实例,必须用 shallowRef
let editorRef = shallowRef();
let handleCreated = (editor: any) => {
  editorRef.value = editor; // 记录 editor 实例,重要!
  newsDetails().then(() => {
    if (newEditsList.value.news_content) {
        //回显
      editor.setHtml(newEditsList.value.news_content);
    }
  });
};

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值