在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

相关文章

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注

Python设置Cookie永不超时的详细指南

《Python设置Cookie永不超时的详细指南》Cookie是一种存储在用户浏览器中的小型数据片段,用于记录用户的登录状态、偏好设置等信息,下面小编就来和大家详细讲讲Python如何设置Cookie... 目录一、Cookie的作用与重要性二、Cookie过期的原因三、实现Cookie永不超时的方法(一)

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客

Linux中压缩、网络传输与系统监控工具的使用完整指南

《Linux中压缩、网络传输与系统监控工具的使用完整指南》在Linux系统管理中,压缩与传输工具是数据备份和远程协作的桥梁,而系统监控工具则是保障服务器稳定运行的眼睛,下面小编就来和大家详细介绍一下它... 目录引言一、压缩与解压:数据存储与传输的优化核心1. zip/unzip:通用压缩格式的便捷操作2.

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

Python中注释使用方法举例详解

《Python中注释使用方法举例详解》在Python编程语言中注释是必不可少的一部分,它有助于提高代码的可读性和维护性,:本文主要介绍Python中注释使用方法的相关资料,需要的朋友可以参考下... 目录一、前言二、什么是注释?示例:三、单行注释语法:以 China编程# 开头,后面的内容为注释内容示例:示例:四

Spring Security中用户名和密码的验证完整流程

《SpringSecurity中用户名和密码的验证完整流程》本文给大家介绍SpringSecurity中用户名和密码的验证完整流程,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定... 首先创建了一个UsernamePasswordAuthenticationTChina编程oken对象,这是S

Linux中SSH服务配置的全面指南

《Linux中SSH服务配置的全面指南》作为网络安全工程师,SSH(SecureShell)服务的安全配置是我们日常工作中不可忽视的重要环节,本文将从基础配置到高级安全加固,全面解析SSH服务的各项参... 目录概述基础配置详解端口与监听设置主机密钥配置认证机制强化禁用密码认证禁止root直接登录实现双因素

Go语言数据库编程GORM 的基本使用详解

《Go语言数据库编程GORM的基本使用详解》GORM是Go语言流行的ORM框架,封装database/sql,支持自动迁移、关联、事务等,提供CRUD、条件查询、钩子函数、日志等功能,简化数据库操作... 目录一、安装与初始化1. 安装 GORM 及数据库驱动2. 建立数据库连接二、定义模型结构体三、自动迁

ModelMapper基本使用和常见场景示例详解

《ModelMapper基本使用和常见场景示例详解》ModelMapper是Java对象映射库,支持自动映射、自定义规则、集合转换及高级配置(如匹配策略、转换器),可集成SpringBoot,减少样板... 目录1. 添加依赖2. 基本用法示例:简单对象映射3. 自定义映射规则4. 集合映射5. 高级配置匹