在Vue中使用wangeditor创建富文本编辑器的完整指南

本文主要是介绍在Vue中使用wangeditor创建富文本编辑器的完整指南,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

🌟 前言

欢迎来到我的技术小宇宙!🌌 这里不仅是我记录技术点滴的后花园,也是我分享学习心得和项目经验的乐园。📚 无论你是技术小白还是资深大牛,这里总有一些内容能触动你的好奇心。🔍

  • 🤖 洛可可白:个人主页

  • 🔥 个人专栏:✅前端技术 ✅后端技术

  • 🏠 个人博客:洛可可白博客

  • 🐱 代码获取:bestwishes0203

  • 📷 封面壁纸:洛可可白wallpaper

在这里插入图片描述

文章目录

  • 在Vue中使用wangeditor创建富文本编辑器的完整指南
      • 效果图
      • 步骤 1: 安装 `wangeditor`
      • 步骤 2: 引入 `wangeditor` 到您的组件
      • 步骤 3: 创建编辑器实例和响应式数据
      • 步骤 4: 在模板中添加编辑器容器
      • 步骤 5: 配置 `wangeditor`(可选)
      • 步骤 6: 获取编辑器内容(可选)
      • 步骤 7: 清理资源
      • 全部代码
    • 🎉 往期精彩回顾

在Vue中使用wangeditor创建富文本编辑器的完整指南

效果图

wangeditor 官网指南

在这里插入图片描述

在Vue项目中使用wangeditor构建富文本编辑器,您需要遵循以下步骤来集成和使用这个流行的编辑器:

步骤 1: 安装 wangeditor

首先,您需要在Vue项目中安装wangeditor。可以通过npm来完成安装:

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

步骤 2: 引入 wangeditor 到您的组件

在您希望使用富文本编辑器的Vue组件中,引入wangeditor

import '@wangeditor/editor/dist/css/style.css';// 引入编辑器的CSS样式
import { onBeforeUnmount, ref, shallowRef, onMounted } from 'vue';
import { Editor, Toolbar } from '@wangeditor/editor-for-vue';

步骤 3: 创建编辑器实例和响应式数据

在Vue组件的mounted生命周期钩子中,创建wangeditor的实例,并将其绑定到指定的DOM元素上:

export default {components: { Editor, Toolbar },setup() {// 编辑器实例,必须用 shallowRef,重要!const editorRef = shallowRef();// 内容 HTMLconst valueHtml = ref('<p>hello</p>');// 模拟 ajax 异步获取内容onMounted(() => {setTimeout(() => {valueHtml.value = '<p>模拟 Ajax 异步设置内容</p>';}, 1500);});const toolbarConfig = {};const editorConfig = { placeholder: '请输入内容...' };// 组件销毁时,也及时销毁编辑器,重要!onBeforeUnmount(() => {const editor = editorRef.value;if (editor == null) return;editor.destroy();});// 编辑器回调函数const handleCreated = (editor) => {console.log('created', editor);editorRef.value = editor; // 记录 editor 实例,重要!};const handleChange = (editor) => {console.log('change:', editor.getHtml());};const handleDestroyed = (editor) => {console.log('destroyed', editor);};const handleFocus = (editor) => {console.log('focus', editor);};const handleBlur = (editor) => {console.log('blur', editor);};const customAlert = (info, type) => {alert(`【自定义提示】${type} - ${info}`);};const customPaste = (editor, event, callback) => {console.log('ClipboardEvent 粘贴事件对象', event);// 自定义插入内容editor.insertText('xxx');// 返回值(注意,vue 事件的返回值,不能用 return)callback(false); // 返回 false ,阻止默认粘贴行为// callback(true) // 返回 true ,继续默认的粘贴行为};const insertText = () => {const editor = editorRef.value;if (editor == null) return;editor.insertText('hello world');};const printHtml = () => {const editor = editorRef.value;if (editor == null) return;console.log(editor.getHtml());};const disable = () => {const editor = editorRef.value;if (editor == null) return;editor.disable()};return {editorRef,mode: 'default',valueHtml,toolbarConfig,editorConfig,handleCreated,handleChange,handleDestroyed,handleFocus,handleBlur,customAlert,customPaste,insertText,printHtml,disable};},
};

步骤 4: 在模板中添加编辑器容器

在Vue组件的模板中,添加一个容器元素来承载wangeditor

    <div style="border: 1px solid #ccc; margin-top: 10px"><Toolbar:editor="editorRef":defaultConfig="toolbarConfig":mode="mode"style="border-bottom: 1px solid #ccc"/><Editor:defaultConfig="editorConfig":mode="mode"v-model="valueHtml"style="height: 400px; overflow-y: hidden"@onCreated="handleCreated"@onChange="handleChange"@onDestroyed="handleDestroyed"@onFocus="handleFocus"@onBlur="handleBlur"@customAlert="customAlert"@customPaste="customPaste"/></div>

步骤 5: 配置 wangeditor(可选)

wangeditor提供了多种配置选项,您可以根据需要进行配置。例如,设置编辑器的自定义菜单、上传图片的接口等:

// const editorConfig = { placeholder: '请输入内容...' };// 初始化 MENU_CONF 属性const editorConfig = {                       // JS 语法MENU_CONF: {},placeholder: '请输入内容...'// 其他属性...}// 修改 uploadImage 菜单配置editorConfig.MENU_CONF['uploadImage'] = {server: '/api/upload-image',fieldName: 'custom-field-name'// 继续写其他配置...//【注意】不需要修改的不用写,wangEditor 会去 merge 当前其他配置}

在这里插入图片描述

步骤 6: 获取编辑器内容(可选)

您可以通过editor.txt.html()方法获取编辑器的HTML内容,或者通过editor.txt.text()获取纯文本内容:

    const printHtml = () => {const editor = editorRef.value;if (editor == null) return;console.log(editor.getHtml());};

步骤 7: 清理资源

当组件销毁时,确保释放编辑器资源,避免内存泄漏:

    // 组件销毁时,也及时销毁编辑器,重要!onBeforeUnmount(() => {const editor = editorRef.value;if (editor == null) return;editor.destroy();});

全部代码

<template><div><div><button @click="insertText">insert text</button><button @click="printHtml">print html</button><button @click="disable">disable</button></div><div style="border: 1px solid #ccc; margin-top: 10px"><Toolbar :editor="editorRef" :defaultConfig="toolbarConfig" :mode="mode" style="border-bottom: 1px solid #ccc" /><Editor :defaultConfig="editorConfig" :mode="mode" v-model="valueHtml" style="height: 400px; overflow-y: hidden"@onCreated="handleCreated" @onChange="handleChange" @onDestroyed="handleDestroyed" @onFocus="handleFocus"@onBlur="handleBlur" @customAlert="customAlert" @customPaste="customPaste" /></div><div style="margin-top: 10px"><textarea v-model="valueHtml" readonly style="width: 100%; height: 200px; outline: none"></textarea></div></div>
</template><script>
import '@wangeditor/editor/dist/css/style.css';
import { onBeforeUnmount, ref, shallowRef, onMounted } from 'vue';
import { Editor, Toolbar } from '@wangeditor/editor-for-vue';export default {components: { Editor, Toolbar },setup() {// 编辑器实例,必须用 shallowRef,重要!const editorRef = shallowRef();// 内容 HTMLconst valueHtml = ref('<p>hello</p>');// 模拟 ajax 异步获取内容onMounted(() => {setTimeout(() => {valueHtml.value = '<p>模拟 Ajax 异步设置内容</p>';}, 1500);});const toolbarConfig = {};// const editorConfig = { placeholder: '请输入内容...' };// 初始化 MENU_CONF 属性const editorConfig = {                       // JS 语法MENU_CONF: {},placeholder: '请输入内容...'// 其他属性...}// 修改 uploadImage 菜单配置editorConfig.MENU_CONF['uploadImage'] = {server: '/api/upload-image',fieldName: 'custom-field-name'// 继续写其他配置...//【注意】不需要修改的不用写,wangEditor 会去 merge 当前其他配置}// 组件销毁时,也及时销毁编辑器,重要!onBeforeUnmount(() => {const editor = editorRef.value;if (editor == null) return;editor.destroy();});// 编辑器回调函数const handleCreated = (editor) => {console.log('created', editor);editorRef.value = editor; // 记录 editor 实例,重要!};const handleChange = (editor) => {console.log('change:', editor.getHtml());};const handleDestroyed = (editor) => {console.log('destroyed', editor);};const handleFocus = (editor) => {console.log('focus', editor);};const handleBlur = (editor) => {console.log('blur', editor);};const customAlert = (info, type) => {alert(`【自定义提示】${type} - ${info}`);};const customPaste = (editor, event, callback) => {console.log('ClipboardEvent 粘贴事件对象', event);// 自定义插入内容editor.insertText('xxx');// 返回值(注意,vue 事件的返回值,不能用 return)callback(false); // 返回 false ,阻止默认粘贴行为// callback(true) // 返回 true ,继续默认的粘贴行为};const insertText = () => {const editor = editorRef.value;if (editor == null) return;editor.insertText('hello world');};const printHtml = () => {const editor = editorRef.value;if (editor == null) return;console.log(editor.getHtml());};const disable = () => {const editor = editorRef.value;if (editor == null) return;editor.disable()};return {editorRef,mode: 'default',valueHtml,toolbarConfig,editorConfig,handleCreated,handleChange,handleDestroyed,handleFocus,handleBlur,customAlert,customPaste,insertText,printHtml,disable};},
};
</script>

通过以上步骤,您可以在Vue项目中轻松地集成和使用wangeditor作为富文本编辑器。wangeditor提供了丰富的功能和良好的定制性,可以满足大多数富文本编辑的需求。

🎉 往期精彩回顾

Vue项目中使用ECharts构建交互式中国地图的详细指南

  • 598阅读 · 12点赞 · 6收藏

米哈游一面前端开发岗面试题,你会做几道?

  • 887阅读 · 21点赞 · 15收藏

程序员必备开发工具、程序员必备集成开发环境(IDE)

  • 635阅读 · 14点赞 · 8收藏

Linux常用操作命令和服务器硬件基础知识

  • 841阅读 · 28点赞 · 9收藏

C语言中大小写字母如何转化

  • 681阅读 · 25点赞 · 27收藏

主流开发语言和开发环境、程序员如何选择职业赛道?

  • 1015阅读 · 34点赞 · 16收藏

Spring Boot+Vue前后端分离项目如何部署到服务器

  • 1048阅读 · 30点赞 · 25收藏

Spring Cloud原理详解、Spring Cloud发展历程

  • 1036阅读 · 32点赞 · 9收藏

爬虫基本原理介绍、实现及问题解决、爬虫实战、爬取经典moba游戏英雄列表

  • 799阅读 · 22点赞 · 21收藏

前端开发的发展史:框架与技术栈的演变

  • 980阅读 · 18点赞 · 12收藏

打字通小游戏制作教程:用HTML5和JavaScript提升打字速度

  • 1196阅读 · 31点赞 · 25收藏

扫雷小游戏制作教程:用HTML5和JavaScript打造经典游戏

  • 1040阅读 · 19点赞 · 27收藏

拼图小游戏制作教程:用HTML5和JavaScript打造经典游戏

  • 762阅读 · 10点赞 · 19收藏

这篇关于在Vue中使用wangeditor创建富文本编辑器的完整指南的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Python中模块graphviz使用入门

《Python中模块graphviz使用入门》graphviz是一个用于创建和操作图形的Python库,本文主要介绍了Python中模块graphviz使用入门,具有一定的参考价值,感兴趣的可以了解一... 目录1.安装2. 基本用法2.1 输出图像格式2.2 图像style设置2.3 属性2.4 子图和聚

windows和Linux使用命令行计算文件的MD5值

《windows和Linux使用命令行计算文件的MD5值》在Windows和Linux系统中,您可以使用命令行(终端或命令提示符)来计算文件的MD5值,文章介绍了在Windows和Linux/macO... 目录在Windows上:在linux或MACOS上:总结在Windows上:可以使用certuti

CentOS和Ubuntu系统使用shell脚本创建用户和设置密码

《CentOS和Ubuntu系统使用shell脚本创建用户和设置密码》在Linux系统中,你可以使用useradd命令来创建新用户,使用echo和chpasswd命令来设置密码,本文写了一个shell... 在linux系统中,你可以使用useradd命令来创建新用户,使用echo和chpasswd命令来设

Python使用Matplotlib绘制3D曲面图详解

《Python使用Matplotlib绘制3D曲面图详解》:本文主要介绍Python使用Matplotlib绘制3D曲面图,在Python中,使用Matplotlib库绘制3D曲面图可以通过mpl... 目录准备工作绘制简单的 3D 曲面图绘制 3D 曲面图添加线框和透明度控制图形视角Matplotlib

Pandas中统计汇总可视化函数plot()的使用

《Pandas中统计汇总可视化函数plot()的使用》Pandas提供了许多强大的数据处理和分析功能,其中plot()函数就是其可视化功能的一个重要组成部分,本文主要介绍了Pandas中统计汇总可视化... 目录一、plot()函数简介二、plot()函数的基本用法三、plot()函数的参数详解四、使用pl

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

使用Java将各种数据写入Excel表格的操作示例

《使用Java将各种数据写入Excel表格的操作示例》在数据处理与管理领域,Excel凭借其强大的功能和广泛的应用,成为了数据存储与展示的重要工具,在Java开发过程中,常常需要将不同类型的数据,本文... 目录前言安装免费Java库1. 写入文本、或数值到 Excel单元格2. 写入数组到 Excel表格

redis中使用lua脚本的原理与基本使用详解

《redis中使用lua脚本的原理与基本使用详解》在Redis中使用Lua脚本可以实现原子性操作、减少网络开销以及提高执行效率,下面小编就来和大家详细介绍一下在redis中使用lua脚本的原理... 目录Redis 执行 Lua 脚本的原理基本使用方法使用EVAL命令执行 Lua 脚本使用EVALSHA命令

Java 中的 @SneakyThrows 注解使用方法(简化异常处理的利与弊)

《Java中的@SneakyThrows注解使用方法(简化异常处理的利与弊)》为了简化异常处理,Lombok提供了一个强大的注解@SneakyThrows,本文将详细介绍@SneakyThro... 目录1. @SneakyThrows 简介 1.1 什么是 Lombok?2. @SneakyThrows

使用Python和Pyecharts创建交互式地图

《使用Python和Pyecharts创建交互式地图》在数据可视化领域,创建交互式地图是一种强大的方式,可以使受众能够以引人入胜且信息丰富的方式探索地理数据,下面我们看看如何使用Python和Pyec... 目录简介Pyecharts 简介创建上海地图代码说明运行结果总结简介在数据可视化领域,创建交互式地