vue3使用wangeditor上传附件以及添加表格,可以直接复制粘贴excel内容

下载依赖包

javascript 复制代码
npm install @wangeditor/editor --save
上传excel,word等文件插件
npm install @wangeditor/plugin-upload-attachment

引入

javascript 复制代码
import '@wangeditor/editor/dist/css/style.css';
import { Editor, Toolbar } from '@wangeditor/editor-for-vue';
import { Boot } from '@wangeditor/editor'
import UploadAttachmentModule from '@wangeditor/plugin-upload-attachment'

dom结构

javascript 复制代码
<div>
	<Toolbar
    :editor="editorRef"
    :defaultConfig="toolbarConfig"
    :mode="'default'"
    style="border-bottom: 1px solid #ccc"
/>
<Editor
    :defaultConfig="editorConfig"
    :mode="'default'"
    v-model="state.ruleForm.nr"
    style="height: 400px; overflow-y: hidden"
    @onCreated="handleCreated"
    @customPaste="customPaste"
/>
</div>

方法配置

javascript 复制代码
const editorRef = shallowRef();
// 插件注册,只需要注册一次即可
onMounted(() => {
    Boot.registerModule(UploadAttachmentModule)
})
const toolbarConfig = {
    insertKeys: {
        index: 21, // 自定义插入的位置
        keys: ['uploadAttachment'], // "上传附件"菜单
    },
};
const editorConfig = {
    placeholder: '请输入内容...',
    hoverbarKeys: {
        attachment: {
            menuKeys: ['downloadAttachment'], // "下载附件"菜单
        },
    },
    MENU_CONF: {
    	//	上传文件配置
        uploadAttachment: {
            server: '上传接口',
            fieldName: 'files', // 上传文件名称
            headers: {
                Authorization: 'token'
            },
            customInsert(res:any, file:any, insertFn:any) {
            	// 回显
                insertFn(file.name, res.data)
            }
        },
        // 上传图片
        uploadImage: {
            server: '上传接口',
            fieldName: 'files', // 上传文件名称
            meta: {
                Authorization: 'token'
            },
            headers: {
                Authorization: getToken()
            },
            customInsert(res:any, insertFn:any) {
            	// 回显
                insertFn(res.data)
            }
        },
    }
};
// 组件销毁时,也及时销毁编辑器,重要!
onBeforeUnmount(() => {
    const editor = editorRef.value;
    if (editor == null) return;

    editor.destroy();
});
// 可控制复制粘贴显示内容
const customPaste = (editor:any, event:any, callback:any) => {
    //   console.log('ClipboardEvent 粘贴事件对象', event);

    // 自定义插入内容
    // editor.insertText('xxx');

    // 返回值(注意,vue 事件的返回值,不能用 return)
    // callback(text); // 返回 false ,阻止默认粘贴行为
    callback(true) // 返回 true ,继续默认的粘贴行为
};

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

demo: https://stackblitz.com/edit/vue3-wangeditor-demo?file=src%2Fcomponents%2FBasicEditor.vue

插件: https://www.wangeditor.com/v5/plugins.html

相关推荐
RuoyiOffice3 天前
2026最推荐基于SpringBoot+Vue3的项目管理经营一体化:从商机、合同、任务与工时走到回款和利润
spring boot·项目管理·vue3·crm·合同管理·ruoyi office·项目经营
RuoyiOffice4 天前
SpringBoot+Vue3 节日主题换肤实战:一条参数换全站配色,节后自动还原
spring boot·vue3·spring boot 3·vben admin·ruoyi office·节日主题·换肤方案
RuoyiOffice6 天前
SpringBoot3+Vue3 流程抄送已读:打开详情消红点,待办与知会怎么分开
spring boot·vue3·flowable·spring boot 3·ruoyi office·流程抄送·已读回执
志尊宝7 天前
Vue3 零基础每日笔记(038):亲手封装 useDebounceFn 与 useThrottleFn——高频事件的流量阀
前端·javascript·vue·html·vue3
志尊宝7 天前
Vue3 零基础每日笔记(043):defineModel 与 defineExpose 的 TS 用法——3.4+ 新 API 进阶
前端·javascript·vue·html·vue3
RuoyiOffice8 天前
SpringBoot3+Vue3+Flowable 企业审批平台全链路:从表单设计、节点权限到移动审批怎么落地
spring boot·vue3·uniapp·springboot3·flowable·审批系统·ruoyi office
RuoyiOffice8 天前
SpringBoot3+Vue3 企业级管理平台架构:多租户、RBAC、数据权限、工作流与微服务一次讲透
spring boot·vue3·springboot3·rbac·多租户·数据权限·ruoyi office
Bs_MoneyMagnet8 天前
基于springboot+vue的个人健康管理系统的设计与实现 源码+文档
java·vue.js·spring boot·后端·vue3·springboot3·计算机毕业设计
志尊宝9 天前
Vue3 零基础每日笔记(026):emit 子传父——defineEmits 与事件校验
前端·javascript·html·vue3
RuoyiOffice9 天前
SpringBoot3+Vue3 库存盘点:账面和实盘对不上,差异单怎么走审批再调账
spring boot·vue3·erp·spring boot 3·库存盘点·盘盈盘亏