【前端】富文本编辑器插件 wangEditor 5 基本使用(Vue2)

在这里插入图片描述
在这里插入图片描述

https://www.wangeditor.com/v5

一、安装

首先安装editor

yarn add @wangeditor/editor
# 或者 npm install @wangeditor/editor --save

安装Vue2组件

yarn add @wangeditor/editor-for-vue
# 或者 npm install @wangeditor/editor-for-vue --save

或者Vue3

yarn add @wangeditor/editor-for-vue@next
# 或者 npm install @wangeditor/editor-for-vue@next --save

二、复制样式

安装完成后,复制 node_modules/@wangeditor/core/dist/css/style.css 文件到src/assets/wangeditor路径下,在main.js

import '@wangeditor/editor/dist/css/style.css'

webpack或者Vite可以直接根据此解析路径,即可加载了。

也可以局部引入:
在组件/页面内

import '@wangeditor/editor/dist/css/style.css'

三、封装组件

1.模板内引入工具栏和编辑框:

<template><div style="border: 1px solid #ccc;"><Toolbar ref="toolbar" style="border-bottom: 1px solid #ccc;" :editor="editor" :defaultConfig="toolbarConfig" :mode="mode" /><Editor style="height: 300px;width: 80%; overflow-y: hidden;" :value="value":defaultConfig="editorConfig" :mode="mode" @onCreated="onCreated" @onChange="onEditorChange" /></div>
</template>
<script>
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
</script>

2.配置工具栏

因为我需要上传附件的按钮,所以先下载附件:

yarn add @wangeditor/plugin-upload-attachment
# npm install @wangeditor/plugin-upload-attachment --save

下载好了之后引入并注册:
(只注册一次,在组件上方)

import { Boot } from '@wangeditor/editor'
import attachmentModule from '@wangeditor/plugin-upload-attachment'
Boot.registerModule(attachmentModule)

后面配置工具栏toolbarConfig
excludeKeys写不想要的工具项,insertKeys写自定义插入的项,mode设置成default即有20+个常用工具项,在此基础上修改即可。

toolbarConfig:{excludeKeys: ['fullScreen',"group-video"],insertKeys:{index: 0, // 自定义插入的位置keys: ['uploadAttachment'], // “上传附件”菜单}
} ,

3.配置editor

工具栏是基于editor来的,所以最开始的时候editor为null,后面在editor的created钩子里初始化:

methods:{
onCreated(editor) {this.editor = Object.seal(editor)
},
}

记得销毁:

    beforeDestroy() {this.editor && this.editor.destroy()},
editorConfig: { hoverbarKeys: {attachment: {menuKeys: ['downloadAttachment'], // “下载附件”菜单},},MENU_CONF: {uploadImage: {server: '', // 你的上传urlfieldName: 'file',allowedFileTypes: ['image/*'],async customUpload(file, insertFn) {const form = new FormData()form.append('file', file)// 你可以在此增加formdataconst { data } = await axios.post(' your url'), form)const url = data.urlinsertFn(url, '', '') // 参数:src、alt、href 用来在编辑框中战术}},uploadAttachment: {server: 'url',fieldName: 'file',timeout: 10 * 1000,// 自定义上传customUpload: this.uploadAttachment},}
},async uploadAttachment (file, insertFn) {const form = new FormData()form.append('file', file)try {const { data } = await axios.post('url', form)const url = data.urlinsertFn(file.name,url)} catch (e) {this.$message.error('上传出错')}}

四、代码汇总

<template><div style="border: 1px solid #ccc;"><Toolbar ref="toolbar" style="border-bottom: 1px solid #ccc;" :editor="editor" :defaultConfig="toolbarConfig" :mode="mode" /><Editor style="height: 300px;width: 80%; overflow-y: hidden;" :value="value":defaultConfig="editorConfig" :mode="mode" @onCreated="onCreated" @onChange="onEditorChange" /></div>
</template>
<script>
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
import { Boot } from '@wangeditor/editor'
import axios from 'axios'
import attachmentModule from '@wangeditor/plugin-upload-attachment'Boot.registerModule(attachmentModule)export default {name:'WEditor',components:{Editor, Toolbar},props:{value:{type:String,default:''}},data(){return {toolbarConfig:{excludeKeys: ['fullScreen',"group-video"],insertKeys:{index: 0, // 自定义插入的位置keys: ['uploadAttachment'], // “上传附件”菜单}} ,editor:null,editorConfig: { hoverbarKeys: {attachment: {menuKeys: ['downloadAttachment'], // “下载附件”菜单},},MENU_CONF: {uploadImage: {server: '',fieldName: 'file',allowedFileTypes: ['image/*'],async customUpload(file, insertFn) {const form = new FormData()form.append('file', file)const { data } = await axios.post('url', form)const url = data.urlinsertFn(url, '', '') // 参数:src、alt、href}},uploadAttachment: {server: 'url',fieldName: 'file',timeout: 10 * 1000,// 自定义上传customUpload: this.uploadAttachment},}},mode: 'default',}},created() {},beforeDestroy() {this.editor && this.editor.destroy()},methods:{onCreated(editor) {this.editor = Object.seal(editor)},onEditorChange(editor){this.$emit('input', editor.getHtml())},async uploadAttachment (file, insertFn) {const form = new FormData()form.append('file', file)try {const { data } = await axios.post('url', form)const url = data.urlinsertFn(file.name,url)} catch (e) {this.$message.error('上传出错')}}}
}
</script>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/web/89170.shtml
繁体地址,请注明出处:http://hk.pswp.cn/web/89170.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

自适应哈希索引 和 日志缓冲区

目录 1. 自适应哈希索引在内存中的位置 2. 自适应哈希索引的作用 3. 为什么要创建自适应哈希索引 4. 适应哈希索引的Key -Value如何设置&#xff1f; 5. 日志缓冲区在内存中的位置 6. 日志缓冲区的作用 7. 日志不通过LogBuffer直接写入磁盘不行吗&#xff1f; 1. 自适应哈…

中国旅行社协会在京召开“文旅人工智能应用研讨会”,助力文旅创新发展

7月15日&#xff0c;由中国旅行社协会数字经济专业委员会和在线旅行服务商分会联合主办的“人工智能技术在文旅产业中的应用”研讨会在北京举行。中国旅行社协会副会长、秘书长孙桂珍出席并致辞&#xff0c;中国工程院外籍院士、具身智能机器人专家张建伟、北京第二外国语学院旅…

Linux之Zabbix分布式监控篇(一)

一、概念和特点概念Zabbix是一款开源、免费的监控软件 主要用于7*24*365实时监控网络设置&#xff0c;操作系统&#xff0c;应用程序&#xff0c;网络带宽等资源的运行状态&#xff0c;并且一旦发生异常能够第一时间个SA管理员发送报警信息特点Zabbix是c/s结构&#xff0c;有c…

ZYNQ千兆光通信实战:Tri Mode Ethernet MAC深度解析

—— 从硬件设计到Linux驱动的光通信创新实践** 当ZYNQ遇上光通信 在工业控制、医疗成像和航空航天等领域,抗干扰、长距离传输的光通信技术至关重要。Xilinx ZYNQ-7000系列凭借ARM+FPGA的架构,结合Tri Mode Ethernet MAC (TEMAC) 核心,为千兆光通信提供了完美解决方案。本文…

求不重叠区间总和最大值

例题链接&#xff1a;1051-习题-数学考试_2021秋季算法入门班第一章习题&#xff1a;模拟、枚举、贪心 来源&#xff1a;牛客网 时间限制&#xff1a;C/C/Rust/Pascal 1秒&#xff0c;其他语言2秒 空间限制&#xff1a;C/C/Rust/Pascal 32 M&#xff0c;其他语言64 M 64bit …

【Golang】GORM - GEN工具 快速开始

文章目录建项目建库建表main.gouser.gocompany.go生成效果&#xff08;更进一步&#xff09;自定义dynamic SQL实践官方地址&#xff1a;https://gorm.io/zh_CN/gen/index.html 以mysql为例 建项目 go mod init 项目名称 go mod tidy建库建表 建数据库demo&#xff0c;正常…

飞书 “打破” AI 与协同办公的「黑箱」

文 | 智能相对论作者 | 陈泊丞在协同办公领域&#xff0c;自从有了AI&#xff0c;微软、钉钉、Google Workspace、Salesforce、企业微信、飞书等厂商都试图通过深度整合AI技术&#xff0c;从智能会议、内容创作、数据管理等场景重构办公范式。微软通过Microsoft 365 Copilot将A…

leetcode:674. 最长连续递增序列[动归]

学习要点 练习动归注意不要马虎 题目链接 674. 最长连续递增序列 - 力扣&#xff08;LeetCode&#xff09; 题目描述 解法&#xff1a;动归 class Solution { public:int findLengthOfLCIS(vector<int>& nums) {int n nums.size();if(nums.size() < 1) …

【html常见页面布局】

考拉商城界面效果htmlcss效果 html <!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"><title>Document</ti…

摩尔线程MUSA架构深度调优指南:从CUDA到MUSA的显存访问模式重构原则

点击 “AladdinEdu&#xff0c;同学们用得起的【H卡】算力平台”&#xff0c;H卡级别算力&#xff0c;按量计费&#xff0c;灵活弹性&#xff0c;顶级配置&#xff0c;学生专属优惠。 当国产GPU面临生态壁垒&#xff0c;显存访问效率成为性能突破的关键战场。本文将深入揭示摩尔…

2025江苏省信息安全管理与评估赛项二三阶段任务书

任务 3 网络安全事件响应、数字取证调查、网络安全渗透任务3.1&#xff1a;网络安全事件响应&#xff08;100分&#xff09;X集团的一台存储关键信息的服务器遭受到了黑客的攻击&#xff0c;现在需要你对该服务器进行应急排查&#xff0c;该服务器的系统目录被上传恶意文件&…

核电概念盘中异动,中核科技涨停引领板块热度

今日股市交易时段&#xff0c;核电概念板块表现活跃&#xff0c;中核科技强势涨停&#xff0c;成为市场关注焦点&#xff0c;为核电产业链相关投资与发展增添新的动态信号。中核科技作为核电阀门等关键设备领域的重要企业&#xff0c;其涨停背后&#xff0c;是多重因素共同作用…

《Java语言程序设计》1.2.3复习题

缩写"CPU"代表什么含义?测量CPU速度的单位是什么?中央处理器(Central Processing Unit,CPU)是计算机的大脑。它从内存中获取指令并执行这些指令。CPU通常由两部分组成&#xff1a;控制单元(control unit)和算术/逻辑单元(arithmetic/logic unit)。控制单元用于控制…

【迭代】绘本生成方案迭代2,解决录音播放问题

代码分享】AI辅助编程&#xff1a;动手制作绘本生成器&#xff0c;实现绘本自由 前面分享了生成绘本PDF的方案&#xff0c;只有图片和文字。所以想加上文字的录音播放。 经过一番探索&#xff0c;发现要实现这个功能的可行性高的方案是用户点击播放&#xff0c;需要跳转到浏览…

C++设计模式之创建型模式

1.前言 设计模式一共有23种&#xff0c;主要分为三大类型&#xff1a;创建型&#xff0c;结构型&#xff0c;行为型。本篇文章着重讲解的是创建型一些相关的设计模式 2.单例模式 Singleton 模式是设计模式中最为简单、最为常见、最容易实现&#xff0c;也是最应该熟悉和掌握的…

kubernetes学习笔记(一)

kubernetes学习笔记(一) kubernetes简介 ​ Kubernetes是Google开源的一个容器编排引擎&#xff0c;它支持自动化部署、大规模可伸缩、应用容器化管理。在生产环境中部署一个应用程序时&#xff0c;通常要部署该应用的多个实例以便对应用请求进行负载均衡。 ​ 在Kubernetes…

Eureka实战

1.创建父工程SpringCloudTestSpringCloudTest为父工程&#xff0c;用于引入通用依赖&#xff0c;如spring-boot-starter-web、lombok&#xff0c;这样子工程就可以直接继承&#xff0c;无需重复引入。在dependencyManagement标签中引入和springboot版本对应的springcloud&#…

如何把镜头对焦在超焦距上

要把镜头对焦在超焦距上&#xff0c;可以按照以下步骤操作&#xff1a;1. 计算超焦距 首先需要知道你的镜头参数和相机参数&#xff1a; 焦距 f&#xff08;如 24mm、35mm&#xff09;光圈 N&#xff08;如 f/8、f/11&#xff09;容许弥散圆直径 c&#xff08;与传感器尺寸有关…

idea docker插件连接docker失败

报错org.apache.hc.client5.http.HttpHostConnectException:Connect to http://localhost:2375 [localhost/127.0.0.1, localhost/0:0:0:o:0:0:0:1] failed:Connection refused:getsockopt解决方法&#xff1a;

【后端】.NET Core API框架搭建(6) --配置使用MongoDB

目录 1.添加包 2. 连接配置 2.1.链接字符串 2.2.连接类 3.仓储配置 3.1.仓储实现 3.2.仓储接口 4.获取配置和注册 4.1.添加配置获取方法 4.2.注册 5.常规使用案例 5.1实体 5.2.实现 5.3.接口 5.4.控制器 NET Core 应用程序中使用 MongoDB 有许多好处&#xff0c;尤其是在…