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

相关推荐
floret. 小花6 小时前
Vue3 知识点总结 · 2026-03-27
前端·面试·electron·学习笔记·vue3
梵得儿SHI1 天前
Vue 3 生态工具实战:UI 组件库与表单验证完全指南
前端·ui·vue3·elementplus·表单验证·antdesignvue·veevalidate
A_nanda2 天前
Vue项目升级
前端·vue3·vue2
创梦流浪人3 天前
soli-admin一款开箱即用的RBAC后台项目
java·spring boot·vue3·springsecurity
floret. 小花6 天前
Vue3 + Electron 知识点总结 · 2026-03-21
前端·面试·electron·学习笔记·vue3
木斯佳7 天前
前端八股文面经大全:小红书前端一二面OC(下)·(2026-03-17)·面经深度解析
前端·vue3·proxy·八股·响应式
沙振宇7 天前
【Web】使用Vue3+PlayCanvas开发3D游戏(六)模拟自驾场景SR+3D可视化
前端·游戏·3d·vue3·playcanvas
小鲤鱼ya7 天前
vue3 + ts + uni-app 移动端封装图片上传添加水印
前端·typescript·uni-app·vue3
floret. 小花7 天前
Vue3 知识点总结 · 2026-03-20
前端·面试·electron·学习笔记·vue3