vue2实现wangEditor富文本便捷器的封装使用--真实项目

news/2024/5/19 15:19:48/文章来源:https://blog.csdn.net/weixin_44834981/article/details/137550823

基于wangEditor 5实现一个简单的富文本编辑器组件,实现自定义上传图片。

官网地址:https://www.wangeditor.com/v5/for-frame.html#%E9%85%8D%E7%BD%AE

1. 安装依赖包:

npm i @wangeditor/editor --save
npm i @wangeditor/editor-for-vue --save

2. 封装组件代码:

<template><div class="editor-wrapper"><toolbar:editor="editor":default-config="toolbarConfig":mode="mode"/><editorv-model="html":style="{height: height + 'px'}":default-config="editorConfig":mode="mode"@onCreated="onCreated"@onChange="onChange"/><div v-if="maxlength" class="useful-num">{{ useLen }}/{{ maxlength }}</div></div>
</template><script>
import fileApi from '@/api/file';
import '@wangeditor/editor/dist/css/style.css';
import {Editor, Toolbar} from '@wangeditor/editor-for-vue';export default {name: 'customEditor',components: {Editor,Toolbar},model: {prop: 'value',event: 'change'},props: {value: {type: String,default: ''},maxlength: {type: Number,default: 0},height: {type: [String, Number],default: 300}},watch: {value(val) {this.html = val;}},data() {return {editor: null,html: '',toolbarConfig: { },editorConfig: {placeholder: '请输入内容',MENU_CONF: {uploadImage: {// 自定义图片上传功能customUpload: (resultFile, insertImgFn) => {const formData = new FormData();formData.append('file', resultFile);// 将文件上传至服务器,res.data返回服务器存放文件的urlfileApi.postFileUpload(formData).then(res => {// 插入图片,三个参数分别对应,url alt hrefinsertImgFn(res.data, '', res.data);});}},uploadVideo: {// 自定义视频上传功能customUpload: (resultFile, insertImgFn) => {const formData = new FormData();formData.append('file', resultFile);// 将文件上传至服务器,res.data返回服务器存放文件的urlfileApi.postFileUpload(formData).then(res => {// 插入视频,三个参数分别对应,url alt hrefinsertImgFn(res.data, '', res.data);});}}}},mode: 'default', // or 'simple'useLen: 0};},methods: {onCreated(editor) {this.editor = Object.seal(editor); // 一定要用 Object.seal() ,否则会报错},onChange() {const text = this.editor.getText();// 计算当前输入了多少文字this.useLen = (text || '').length;// 每次富文本内容改变,触发change事件this.$emit('change', this.html);}},beforeDestroy() {// editor销毁const editor = this.editor;if (editor == null) {return;}editor.destroy();}
};
</script><style lang="scss" scoped>.editor-wrapper{z-index: 3;position: relative;     /deep/.w-e-toolbar{z-index: 2!important;border: solid 1px #E6E9EC!important;border-top-left-radius: 6px;border-top-right-radius: 6px;.w-e-bar-item{padding: 1px;}}/deep/.w-e-text-container{z-index: 1!important;border: solid 1px #E6E9EC!important;border-top: none!important;border-bottom-left-radius: 6px;border-bottom-right-radius: 6px;}}.useful-num{position: absolute;right: 6px;bottom: 10px;z-index: 99999;font-size: 12px;color: $text-3;background: #fff;padding: 0 6px;height: 28px;line-height: 28px;}
</style>

3. 父组件使用:

<template><editorv-model="html":maxlength="8192"@change="change"/>
</template><script>
import Editor from '@/components/Editor';
export default {name: 'EditorExample',components: {Editor},data() {return {html: ''};},methods: {change(val) {console.log(val);}}
};
</script>

4. 真实项目使用:

封装了wangEditor富文本组件,并且设置了数据回显;

TextRich/index.vue

<template><div style="border: 1px solid #ccc; width: 100%"><Toolbar style="border-bottom: 1px solid #ccc" :editor="editor" :defaultConfig="toolbarConfig" :mode="mode" /><Editorstyle="height: 500px; overflow-y: hidden"v-model="html":defaultConfig="editorConfig":mode="mode"@onCreated="handleCreated"@onChange="handleChange"/></div>
</template>
<script>
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
import { DomEditor } from '@wangeditor/editor'
import { set } from 'vue'
export default {components: {Editor,Toolbar},props: {value: {type: String,default: ''}},watch: {value(val) {setTimeout(()=>{this.html = val}, 1000)}},data() {return {editor: null,html: '',mode: 'default',editorConfig: {// placeholder: '请输入内容...',backColor: 'red', // 背景颜色MENU_CONF: {// 配置上传图片uploadImage: {customUpload: this.uploaadImg},uploadVideo: {customUpload: this.uploaadVideo}}},toolbarConfig: {}}},mounted() {// // 模拟 ajax 请求,异步渲染编辑器// setTimeout(() => {//   this.html =//     '<p>tupian </p><p>tupian</p><p><br></p><p><img src="https://img1.baidu.com/it/u=608404415,1639465523&fm=253&fmt=auto&app=120&f=JPEG?w=1280&h=800" alt="tupian" data-href="https://img1.baidu.com/it/u=608404415,1639465523&fm=253&fmt=auto&app=120&f=JPEG?w=1280&h=800" style=""/></p>'// }, 1500)},methods: {handleCreated(editor) {this.editor = Object.seal(editor) // 一定要用 Object.seal() ,否则会报错//   设置工具栏详情this.toolbarConfig = {excludeKeys: ['insertVideo', 'uploadVideo', 'group-video', 'fullScreen']}},handleChange(content) {const toolbar = DomEditor.getToolbar(content)//   查看工具栏列表toolbar.getConfig().toolbarKeysthis.$emit('change', this.html)},uploaadImg(file, insertFn) {this.$emit('uploadImg', file, insertFn)},uploaadVideo(file, insertFn) {this.$emit('uploadVideo', file, insertFn)}},beforeDestroy() {const editor = this.editorif (editor == null) returneditor.destroy() // 销毁编辑器}
}
</script>
<style src="@wangeditor/editor/dist/css/style.css"></style>

父组件使用:

父组件是在el-form表中使用的

<template><div class="project-container"><el-form ref="form" :model="ruleForm" label-position="left" class="form"><el-form-item label="复现步骤" prop="reproduceStep" label-width="100px"><RichText v-model="ruleForm.reproduceStep" :readOnlys="readOnlys" @change="richTextChangeData" @uploadImg="richTextUploadImg"></RichText></el-form-item></el-form></div>
</template><script lang="ts">
import Vue from 'vue'
import RichText from '@/components/RichText/index.vue'export default Vue.extend({name: 'Index',components: {RichText},data() {return {ruleForm: {reproduceStep: ''}}},mounted() {this.setFormData()},methods: {richTextChangeData(val: string) {// 获取最新的html数据// this.ruleForm.reproduceStep = val},richTextUploadImg(file: any, insertFn: any) {// 插入图片,调接口返回图片url,通过插入contenglet imgUrl = 'https://img1.baidu.com/it/u=608404415,1639465523&fm=253&fmt=auto&app=120&f=JPEG?w=1280&h=800'insertFn(imgUrl)// 设置只读this.readOnlys = true},setFormData() {this.ruleForm.reproduceStep ='<p>tupian </p><p>tupian</p><p><br></p><p><img src="https://img1.baidu.com/it/u=608404415,1639465523&fm=253&fmt=auto&app=120&f=JPEG?w=1280&h=800" alt="tupian" data-href="https://img1.baidu.com/it/u=608404415,1639465523&fm=253&fmt=auto&app=120&f=JPEG?w=1280&h=800" style=""/></p>'}}
})
</script>

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

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

相关文章

HarmonyOS 开发-数据库版本升级案例

介绍 本示例介绍使用关系型数据库的接口来进行数据库升降级场景实现 效果预览图 使用说明 加载完成后有版本升级以及版本恢复两种按钮点击版本升级下的”升级至V2“按钮&#xff0c;则数据库版本会从V1升级至V2&#xff0c;且在表格处显示V1和V2版本表格字段对比。点击版本升…

CNN-Transformer时间序列预测

部分代码&#xff1a; # CNN-Transformer class CNNTransformerEncoder(nn.Module):def __init__(self, input_features, transformer_encoder_heads,embedding_features, cnn_kernel_size, dim_feedforward_enc, n_encoder_layer):super(CNNTransformerEncoder, self).__init…

AcWing [875]快速幂(C++)

给定 n 组 ai,bi,pi&#xff0c;对于每组数据&#xff0c;求出 ai^bi mod pi 的值。 输入格式 第一行包含整数 n。 接下来 n行&#xff0c;每行包含三个整数 ai,bi,pi。 输出格式 对于每组数据&#xff0c;输出一个结果&#xff0c;表示 ai^bi mod pi 的值。 每个结果占一…

提升Terraform工作流程最佳实践

Terraform 是管理基础设施及代码&#xff08;IaC&#xff09;最常用的工具之一&#xff0c;它能使我们安全且可预测地对基础设施应用更改。刚开始上手 Terraform 可能会感觉有些不容易&#xff0c;但很快就能对该工具有基本的了解&#xff0c;随之可以开始运行命令、创建和重构…

python爬虫 爬取网页图片

http://t.csdnimg.cn/iQgHw //爬虫爬取图片其实是很简单的&#xff0c;但是大多数同学&#xff0c;可能对 url的设置一直有困惑&#xff08;这点本人也在研究&#xff09;&#xff0c;而本篇文章&#xff0c;对于想要爬取图片的小白简直是福利。你只需要将文章代码运行即可&am…

详细分析Vuex中的mapGetters

目录 1. 基本知识2. Demo13. Demo2 1. 基本知识 优势和用途 简化代码&#xff1a;用 mapGetters 和 mapState&#xff0c;可以简化组件中对于 Vuex 中状态和 getter 的映射工作&#xff0c;减少了重复的代码书写更易读&#xff1a;组件中直接使用映射的计算属性&#xff0c;使…

基于SSM+Jsp+Mysql的快递管理系统

开发语言&#xff1a;Java框架&#xff1a;ssm技术&#xff1a;JSPJDK版本&#xff1a;JDK1.8服务器&#xff1a;tomcat7数据库&#xff1a;mysql 5.7&#xff08;一定要5.7版本&#xff09;数据库工具&#xff1a;Navicat11开发软件&#xff1a;eclipse/myeclipse/ideaMaven包…

h5页面弹窗

div <div v-if"isShowQR" class"QRAlert"><div class"QR-container"><div class"QR-code-bg"><div class"title" v-if"isApp">{{selectedItem.title}}APP下载</div><div class…

JUC:实现一个简易的数据库连接池(享元模式)

主要是学习享元模式。 享元模式&#xff08;Flyweight Pattern&#xff09;是一种结构型设计模式&#xff0c;旨在通过共享尽可能多的对象来最小化内存使用和提高性能。在该模式中&#xff0c;对象被分为两种状态&#xff1a;内部状态和外部状态。 内部状态&#xff08;Intr…

烤羊肉串引来的思考--命令模式

1.1 吃羊肉串&#xff01; 烧烤摊旁边等着拿肉串的人七嘴八舌地叫开了。场面有些混乱&#xff0c;由于人实在太多&#xff0c;烤羊肉串的老板已经分不清谁是谁&#xff0c;造成分发错误&#xff0c;收钱错误&#xff0c;烤肉质量不过关等。 外面打游击烤羊肉串和这种开门店做烤…

FIR滤波器(汉宁窗设计)的MATLAB代码

FIR滤波器&#xff0c;即有限脉冲响应滤波器&#xff08;Finite Impulse Response Filter&#xff09;&#xff0c;是数字信号处理中的一种滤波器。它的特点在于&#xff0c;滤波器的单位冲激响应是有限长度的&#xff0c;这意味着当给定一个输入信号后&#xff0c;输出信号只会…

自动驾驶传感器:传感的本质

自动驾驶传感器&#xff1a;传感的本质 附赠自动驾驶学习资料和量产经验&#xff1a;链接 0. 前言 这个系列的背景是&#xff1a;工作时候需要攒一台数据采集车辆&#xff0c;那段时间需要熟悉感知硬件&#xff0c;写了不少笔记&#xff0c;都是些冗长的文章&#xff0c;感兴…

【电路笔记】-逻辑非门

逻辑非门 文章目录 逻辑非门1、概述2、晶体管逻辑非门3、六角施密特反相器逻辑非门是所有逻辑门中最基本的,通常称为反相缓冲器或简称为反相器。 1、概述 反相非门是单输入器件,其输出电平通常为逻辑电平“1”,当其单个输入为逻辑电平“1”时,输出电平变为“低”至逻辑电平…

意得辑意得辑

你是否也曾遇到过在发表论文时英语写作水平不尽如人意的困境&#xff1f;审稿意见总是指出语言表达不够好&#xff0c;需要找英语母语者修改&#xff1f;不用担心&#xff0c;我和你一样&#xff0c;也曾历经这样的挑战。但是&#xff0c;我找到了一家值得信赖的专业润色机构—…

读书笔记之人生算法(7)

孤独、爆仓与迷信 跨越出身和运气&#xff0c;实现富足与自由&#xff0c;用概率思维做好决策 13 孤独 孤独&#xff1a;获得好姻缘的算法 姻缘是奇妙的东西&#xff0c;体现了世界的随机性&#xff1a;即使是最理性的人&#xff0c;也可能需要靠运气寻找另一半。 中国有句古话…

使用手动连接,将登录框中的取消按钮使用qt4版本的连接到自定义的槽函数中,在自定义的槽函数中调用关闭函数

使用手动连接&#xff0c;将登录框中的取消按钮使用qt4版本的连接到自定义的槽函数中&#xff0c;在自定义的槽函数中调用关闭函数 将登录按钮使用qt4版本的连接到自定义的槽函数中&#xff0c;在槽函数中判断ui界面上输入的账号是否为"admin"&#xff0c;密码是否为…

Nginx入门详解

一、概述 1.1问题背景 小公司项目刚刚上线的时候&#xff0c;并发量小&#xff0c;用户使用的少&#xff0c;所以在低并发的情况下&#xff0c;一个jar包启动应用就够了&#xff0c;然后内部tomcat返回内容给用户。 但是慢慢的&#xff0c;使用平台的用户越来越多了&#xff…

Python基于深度学习的动物图片识别技术的研究与实现

博主介绍&#xff1a;✌程序员徐师兄、7年大厂程序员经历。全网粉丝12w、csdn博客专家、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和毕业项目实战✌ &#x1f345;文末获取源码联系&#x1f345; &#x1f447;&#x1f3fb; 精彩专栏推荐订阅&#x1f447;…

使用jQuery实现购物界面的动态效果

实现功能&#xff1a;&#xff08;购物车以表格的格式展示&#xff09; 1 全选框和复选框之间的联动关系&#xff1a; 点击全选&#xff0c;所有复选框checked状态为true 点击复选框&#xff0c;全选框状态实时更新 2 点击删除按钮&#xff0c;删除对应的行 3 点击删除所选…

CANoe自带的TCP/IP协议栈中TCP的keep alive机制是如何工作的

TCP keep alive机制我们已经讲过太多次,车内很多控制器的TCP keep alive机制相信很多开发和测试的人也配置或者测试过。我们今天想知道CANoe软件自带的TCP/IP协议栈中TCP keep alive机制是如何工作的。 首先大家需要知道TCP keep alive的参数有哪些?其实就三个参数:CP_KEEP…