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

相关推荐
海市公约8 小时前
Vue3组合式API与响应式系统核心机制详解
vue3·computed·reactive·ref·响应式系统·composition api·script setup
小茴香3531 天前
Vue3路由权限动态管理
前端·前端框架·vue3
暗冰ཏོ5 天前
《2026 Vue2 + Vue3 完整学习指南:基础语法、路由缓存、登录拦截、项目实战与面试题》
前端·vue.js·vue·vue3·vue2
曲幽6 天前
写页面时别再把 Element Plus 整个搬进来啦!Vue3按需加载的坑我帮你踩平了
vue3·web·vite·icon·element plus·vs code·import·unplugin
小云小白7 天前
若依-vue3 把深色版本改成天蓝色-含登录页
vue3·若依·天蓝色
曲幽8 天前
FastApiAdmin 后端接口开发好了,前端管理界面怎么调用与显示?
python·vue3·api·fastapi·web·ant design·view·menu·frontend
曲幽12 天前
我用了FastApiAdmin后,连夜把踩过的坑都整理出来了
redis·python·postgresql·vue3·fastapi·web·sqlalchemy·admin·fastapiadmin
Liu.77413 天前
vue3bug收录
vue3
Asurplus18 天前
【VUE】16、使用 wangEditor 富文本编辑器
vue.js·图片上传·wangeditor·富文本编辑器
小云小白22 天前
高性能 v-html 弹窗实现:Vue3 + Element Plus 最佳实践
vue3·弹窗·v-html