vue2使用富文本wangeditor

2024-01-13 05:52

本文主要是介绍vue2使用富文本wangeditor,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

安装

npm i wangeditor --save

引用

import E from 'wangeditor';

使用

        // 富文本初始化initEditor() {this.isEdit = true;this.$nextTick(() => {this.editor = new E(this.$refs.editorElem); //绑定节点this.editor.config.height = 550; //默认高度为 300,设置高度时不需要添加单位this.editor.config.zIndex = 100;this.editor.config.placeholder = ''; //当设置为空时,可以清除提示文字this.editor.config.focus = false; //可以取消自动聚焦this.editor.config.showLinkImg = false;this.editor.config.filterJs = false;this.editor.config.uploadImgMaxLength = 1;this.editor.config.uploadImgMaxSize = 2 * 1024 * 1024; // 2Mthis.editor.config.customUploadImg = async file => {console.log(file[0]);const reader = new FileReader();reader.readAsDataURL(file[0]);const formData = new FormData();formData.append('file', file[0]);formData.append('module', 'proposal');const { data } = await Upload(formData);if (data.code === 200) {console.log(data.data.fullPath);this.editor.cmd.do('insertHTML',`<div class="proposal-content" style="text-indent:2em; font-size:16px; line-height:1.5"><img src=${data.data.fullPath} /></div`);}};// 隐藏发送消息的添加视频、表格、代码、分割线功能this.editor.config.excludeMenus = ['head','video','code','splitLine','link','list','todo','foreColor','backColor','quote','emoticon','strikeThrough', // 删除线'underline', // 下划线'italic', // 斜体'bold', // 粗体'fontSize', // 字号'fontName', // 字体];console.log('this.chapterList', this.chapterList);this.$nextTick(() => {if (this.editor == null) {this.editor.create();this.editor.txt.html('');} else {this.editor.destroy(); //判断编辑器是否被创建,如果创建了就先销毁。this.editor.create();this.$nextTick(() => {let content = '';this.chapterList.chapterContent.forEach(item => {if (item.content) {content += item.content.replace(/\n\n\n/g, '<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;').replace(/\n\n/g, '<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');content = content.replace(/\n/g, `<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`);}// 图片回显if (item.image.path) {content += `<div class="img" style="width: 100%;display: flex;flex-direction: column;justify-content: center;align-items: center;"><img src=${item.image.path} /><div style="font-size: 14px;">图${this.chapterList.chapterIndex}-${item.image.index}  <span class="img-title">${item.image.title}</span></div></div><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`;}// 表格回显if (item.table.title) {content += `<div class="table" style="width:100%;display: flex;flex-direction: column;justify-content: center;align-items: center;"><span>表${this.chapterList.chapterIndex}-${item.table.index}  ${item.table.title}</span><table border="1" cellspacing="0" cellpadding="0" style="width: 100%;"><tbody><tr>`;item.table.head.forEach(item => {content += `<th>${item}</th>`;});content += `<tr>`;item.table.content.forEach(item => {content += `<tr>`;item.forEach(item => {content += `<td>${item}</td>`;});content += `</tr>`;});content += `</tbody></table></div><div class="proposal-content" style="text-indent:2em; font-size:16px; line-height:1.5"></div>`;}});content = `<div class="proposal-content" style="text-indent:2em; font-size:16px; line-height:1.5">${content.replace(`<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`, ``).replace(new RegExp('°E。'), `°E。<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`)}</div>`;this.editor.txt.html(content);});}});});},

在data(){}中定义editor:

export default {data() {return {editor: null,};},

然后根据需求调用就好了,

this.$nextTick(()=>{})中处理的数据是因为后端返回来的数据结构是数组,遍历赋值标签转字符串的处理操作,如果后端返回来的是字符串直接赋值就好了。

注意:

要使用this.$nextTick(()=>{}),有时候会有渲染不上报错的问题。

这篇关于vue2使用富文本wangeditor的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/600444

相关文章

使用Python实现矢量路径的压缩、解压与可视化

《使用Python实现矢量路径的压缩、解压与可视化》在图形设计和Web开发中,矢量路径数据的高效存储与传输至关重要,本文将通过一个Python示例,展示如何将复杂的矢量路径命令序列压缩为JSON格式,... 目录引言核心功能概述1. 路径命令解析2. 路径数据压缩3. 路径数据解压4. 可视化代码实现详解1

Pandas透视表(Pivot Table)的具体使用

《Pandas透视表(PivotTable)的具体使用》透视表用于在数据分析和处理过程中进行数据重塑和汇总,本文就来介绍一下Pandas透视表(PivotTable)的具体使用,感兴趣的可以了解一下... 目录前言什么是透视表?使用步骤1. 引入必要的库2. 读取数据3. 创建透视表4. 查看透视表总结前言

Python 交互式可视化的利器Bokeh的使用

《Python交互式可视化的利器Bokeh的使用》Bokeh是一个专注于Web端交互式数据可视化的Python库,本文主要介绍了Python交互式可视化的利器Bokeh的使用,具有一定的参考价值,感... 目录1. Bokeh 简介1.1 为什么选择 Bokeh1.2 安装与环境配置2. Bokeh 基础2

Android使用ImageView.ScaleType实现图片的缩放与裁剪功能

《Android使用ImageView.ScaleType实现图片的缩放与裁剪功能》ImageView是最常用的控件之一,它用于展示各种类型的图片,为了能够根据需求调整图片的显示效果,Android提... 目录什么是 ImageView.ScaleType?FIT_XYFIT_STARTFIT_CENTE

Java学习手册之Filter和Listener使用方法

《Java学习手册之Filter和Listener使用方法》:本文主要介绍Java学习手册之Filter和Listener使用方法的相关资料,Filter是一种拦截器,可以在请求到达Servl... 目录一、Filter(过滤器)1. Filter 的工作原理2. Filter 的配置与使用二、Listen

Pandas使用AdaBoost进行分类的实现

《Pandas使用AdaBoost进行分类的实现》Pandas和AdaBoost分类算法,可以高效地进行数据预处理和分类任务,本文主要介绍了Pandas使用AdaBoost进行分类的实现,具有一定的参... 目录什么是 AdaBoost?使用 AdaBoost 的步骤安装必要的库步骤一:数据准备步骤二:模型

使用Pandas进行均值填充的实现

《使用Pandas进行均值填充的实现》缺失数据(NaN值)是一个常见的问题,我们可以通过多种方法来处理缺失数据,其中一种常用的方法是均值填充,本文主要介绍了使用Pandas进行均值填充的实现,感兴趣的... 目录什么是均值填充?为什么选择均值填充?均值填充的步骤实际代码示例总结在数据分析和处理过程中,缺失数

如何使用 Python 读取 Excel 数据

《如何使用Python读取Excel数据》:本文主要介绍使用Python读取Excel数据的详细教程,通过pandas和openpyxl,你可以轻松读取Excel文件,并进行各种数据处理操... 目录使用 python 读取 Excel 数据的详细教程1. 安装必要的依赖2. 读取 Excel 文件3. 读

解决Maven项目idea找不到本地仓库jar包问题以及使用mvn install:install-file

《解决Maven项目idea找不到本地仓库jar包问题以及使用mvninstall:install-file》:本文主要介绍解决Maven项目idea找不到本地仓库jar包问题以及使用mvnin... 目录Maven项目idea找不到本地仓库jar包以及使用mvn install:install-file基

Python使用getopt处理命令行参数示例解析(最佳实践)

《Python使用getopt处理命令行参数示例解析(最佳实践)》getopt模块是Python标准库中一个简单但强大的命令行参数处理工具,它特别适合那些需要快速实现基本命令行参数解析的场景,或者需要... 目录为什么需要处理命令行参数?getopt模块基础实际应用示例与其他参数处理方式的比较常见问http