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使用FastAPI实现大文件分片上传与断点续传功能

《Python使用FastAPI实现大文件分片上传与断点续传功能》大文件直传常遇到超时、网络抖动失败、失败后只能重传的问题,分片上传+断点续传可以把大文件拆成若干小块逐个上传,并在中断后从已完成分片继... 目录一、接口设计二、服务端实现(FastAPI)2.1 运行环境2.2 目录结构建议2.3 serv

Spring Security简介、使用与最佳实践

《SpringSecurity简介、使用与最佳实践》SpringSecurity是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架,本文给大家介绍SpringSec... 目录一、如何理解 Spring Security?—— 核心思想二、如何在 Java 项目中使用?——

springboot中使用okhttp3的小结

《springboot中使用okhttp3的小结》OkHttp3是一个JavaHTTP客户端,可以处理各种请求类型,比如GET、POST、PUT等,并且支持高效的HTTP连接池、请求和响应缓存、以及异... 在 Spring Boot 项目中使用 OkHttp3 进行 HTTP 请求是一个高效且流行的方式。

Vue和React受控组件的区别小结

《Vue和React受控组件的区别小结》本文主要介绍了Vue和React受控组件的区别小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录背景React 的实现vue3 的实现写法一:直接修改事件参数写法二:通过ref引用 DOMVu

Java使用Javassist动态生成HelloWorld类

《Java使用Javassist动态生成HelloWorld类》Javassist是一个非常强大的字节码操作和定义库,它允许开发者在运行时创建新的类或者修改现有的类,本文将简单介绍如何使用Javass... 目录1. Javassist简介2. 环境准备3. 动态生成HelloWorld类3.1 创建CtC

使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解

《使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解》本文详细介绍了如何使用Python通过ncmdump工具批量将.ncm音频转换为.mp3的步骤,包括安装、配置ffmpeg环... 目录1. 前言2. 安装 ncmdump3. 实现 .ncm 转 .mp34. 执行过程5. 执行结

Java实现将HTML文件与字符串转换为图片

《Java实现将HTML文件与字符串转换为图片》在Java开发中,我们经常会遇到将HTML内容转换为图片的需求,本文小编就来和大家详细讲讲如何使用FreeSpire.DocforJava库来实现这一功... 目录前言核心实现:html 转图片完整代码场景 1:转换本地 HTML 文件为图片场景 2:转换 H

Java使用jar命令配置服务器端口的完整指南

《Java使用jar命令配置服务器端口的完整指南》本文将详细介绍如何使用java-jar命令启动应用,并重点讲解如何配置服务器端口,同时提供一个实用的Web工具来简化这一过程,希望对大家有所帮助... 目录1. Java Jar文件简介1.1 什么是Jar文件1.2 创建可执行Jar文件2. 使用java

C#使用Spire.Doc for .NET实现HTML转Word的高效方案

《C#使用Spire.Docfor.NET实现HTML转Word的高效方案》在Web开发中,HTML内容的生成与处理是高频需求,然而,当用户需要将HTML页面或动态生成的HTML字符串转换为Wor... 目录引言一、html转Word的典型场景与挑战二、用 Spire.Doc 实现 HTML 转 Word1

Vue3绑定props默认值问题

《Vue3绑定props默认值问题》使用Vue3的defineProps配合TypeScript的interface定义props类型,并通过withDefaults设置默认值,使组件能安全访问传入的... 目录前言步骤步骤1:使用 defineProps 定义 Props步骤2:设置默认值总结前言使用T