在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

相关文章

C++中assign函数的使用

《C++中assign函数的使用》在C++标准模板库中,std::list等容器都提供了assign成员函数,它比操作符更灵活,支持多种初始化方式,下面就来介绍一下assign的用法,具有一定的参考价... 目录​1.assign的基本功能​​语法​2. 具体用法示例​​​(1) 填充n个相同值​​(2)

Spring StateMachine实现状态机使用示例详解

《SpringStateMachine实现状态机使用示例详解》本文介绍SpringStateMachine实现状态机的步骤,包括依赖导入、枚举定义、状态转移规则配置、上下文管理及服务调用示例,重点解... 目录什么是状态机使用示例什么是状态机状态机是计算机科学中的​​核心建模工具​​,用于描述对象在其生命

IntelliJ IDEA2025创建SpringBoot项目的实现步骤

《IntelliJIDEA2025创建SpringBoot项目的实现步骤》本文主要介绍了IntelliJIDEA2025创建SpringBoot项目的实现步骤,文中通过示例代码介绍的非常详细,对大家... 目录一、创建 Spring Boot 项目1. 新建项目2. 基础配置3. 选择依赖4. 生成项目5.

PostgreSQL中rank()窗口函数实用指南与示例

《PostgreSQL中rank()窗口函数实用指南与示例》在数据分析和数据库管理中,经常需要对数据进行排名操作,PostgreSQL提供了强大的窗口函数rank(),可以方便地对结果集中的行进行排名... 目录一、rank()函数简介二、基础示例:部门内员工薪资排名示例数据排名查询三、高级应用示例1. 每

Linux线程之线程的创建、属性、回收、退出、取消方式

《Linux线程之线程的创建、属性、回收、退出、取消方式》文章总结了线程管理核心知识:线程号唯一、创建方式、属性设置(如分离状态与栈大小)、回收机制(join/detach)、退出方法(返回/pthr... 目录1. 线程号2. 线程的创建3. 线程属性4. 线程的回收5. 线程的退出6. 线程的取消7.

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定

SpringBoot结合Docker进行容器化处理指南

《SpringBoot结合Docker进行容器化处理指南》在当今快速发展的软件工程领域,SpringBoot和Docker已经成为现代Java开发者的必备工具,本文将深入讲解如何将一个SpringBo... 目录前言一、为什么选择 Spring Bootjavascript + docker1. 快速部署与

深入理解Go语言中二维切片的使用

《深入理解Go语言中二维切片的使用》本文深入讲解了Go语言中二维切片的概念与应用,用于表示矩阵、表格等二维数据结构,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧... 目录引言二维切片的基本概念定义创建二维切片二维切片的操作访问元素修改元素遍历二维切片二维切片的动态调整追加行动态

prometheus如何使用pushgateway监控网路丢包

《prometheus如何使用pushgateway监控网路丢包》:本文主要介绍prometheus如何使用pushgateway监控网路丢包问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录监控网路丢包脚本数据图表总结监控网路丢包脚本[root@gtcq-gt-monitor-prome

Python通用唯一标识符模块uuid使用案例详解

《Python通用唯一标识符模块uuid使用案例详解》Pythonuuid模块用于生成128位全局唯一标识符,支持UUID1-5版本,适用于分布式系统、数据库主键等场景,需注意隐私、碰撞概率及存储优... 目录简介核心功能1. UUID版本2. UUID属性3. 命名空间使用场景1. 生成唯一标识符2. 数