vue3-使用富文本编辑器-wangEditor-文章发表1

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

最近在搞项目:我们组内几位成员正在搞一个网站搭建,以后更新会比较缓慢

引言:如果要网站要用的富文本编辑器的话,这边推荐用wangEditor

官网地址传送 :

wangEditoricon-default.png?t=N7T8https://www.wangeditor.com/

我现在还在扩展我的写文章用的富文本编辑器

现在我将简单介绍一下其基本使用方法

基本模版

安装依赖


npm install @wangeditor/editor --savenpm install @wangeditor/editor-for-vue@next --save

 template部分

<el-form-item  style="background-color: #f5f5f5;><Toolbar class="Toolbar"style="border-bottom: 1px solid #ccc;font-size: 15px":editor="editorRef":defaultConfig="toolbarConfig":mode="mode"/><Editor class="Editor"style="height: 500px;width: 900px;"v-model="valueHtml":defaultConfig="editorConfig":mode="mode"@onCreated="handleCreated"></Editor></el-form-item>

js部分

导包
import { onMounted,onBeforeUnmount, ref, shallowRef } from 'vue'
import '@wangeditor/editor/dist/css/style.css' // 引入 css
import { Editor } from '@wangeditor/editor-for-vue';
import {Toolbar} from '@wangeditor/editor-for-vue' // 假设这是你的工具栏组件
 主体部分
// 编辑器实例,必须用 shallowRef
const editorRef = shallowRef(null)// 内容 HTML
const valueHtml = ref('')
const toolbarConfig = {}
const editorConfig = { placeholder: '请输入内容...' }// 组件销毁时,也及时销毁编辑器
onBeforeUnmount(() => {const editor = editorRef.valueif (editor == null) returneditor.destroy()
})// 监听编辑器创建事件
const handleCreated = (editor) => {editorRef.value = editor // 记录 editor 实例,重要!
}

内容导入

template部分

这边绑定一个v-html样式的div(同步保存Editorde内容)

<div style="margin-top: 20px;"><divid="editor-content-html"style="width: 80px; height: 100px; outline: none; border: 1px solid #ccc; padding: 10px; overflow-y: auto;"v-html="html"></div></div>

js部分 

初始化 响应式数据 
const html =ref('<p>hello</p>')//初始话同步到html中
监听编辑器输入事件

editor.on('change', () => { updateHtml() }) 这行代码是在编辑器创建完成后,给编辑器实例绑定了一个监听器,当编辑器内容发生变化时,会触发这个回调函数,从而调用 updateHtml() 函数来更新 HTML 内容


// 编辑器创建完成时的回调函数
const handleCreated = (editor) => {editorRef.value = editor // 记录 editor 实例,重要!// 监听编辑器输入事件editor.on('change', () => {updateHtml()})
}

 同时更新html内容
const updateHtml = () => {if (editorRef.value) {html.value = editorRef.value.getHtml() // 实时更新 HTML 内容}
}
</sc

优化 

顶部栏的制作

  <el-card style="position:fixed;top: 0 ;height: 60px;width: 100%;display: flex ;"><div><el-button style="font-weight: bold;" > 发表文章</el-button><el-avatar :size="40" src="../img/logo.jpg" style="position: absolute;right:30px" /></div></el-card>

更改编辑区域和工具栏的相关样式

  <Toolbarclass="Toolbar"style="border-bottom: 1px solid #ccc; font-size: 15px; margin: 0 auto;width: 100%; position: fixed; top: 60px;;":editor="editorRef":defaultConfig="toolbarConfig":mode="mode"/><el-card style="margin: 30px auto; padding: 0 40px; display: flex; flex-direction: column; margin-top: 150px;"><div style="margin-bottom: 10px; display: flex"><input type="text" placeholder="标题" style="height: 50px; border: none; outline: none; padding: 10px; font-size: 40px; width: fit-content;"></div><hr><Editorclass="Editor"style="height: 500px; width: 900px; margin: 0 auto; display: flex ;flex-wrap: nowrap"v-model:content="valueHtml":defaultConfig="editorConfig":mode="mode"@onCreated="handleCreated"@change="handleChange"></Editor></el-card>

最终效果

最终代码

<template><el-card style="position:fixed;top: 0 ;height: 60px;width: 100%;display: flex ;"><div><el-button style="font-weight: bold;" > 发表文章</el-button><el-avatar :size="40" src="../img/logo.jpg" style="position: absolute;right:30px" /></div></el-card><el-form-item style="background-color: #f5f5f5; "><Toolbarclass="Toolbar"style="border-bottom: 1px solid #ccc; font-size: 15px; margin: 0 auto;width: 100%; position: fixed; top: 60px;;":editor="editorRef":defaultConfig="toolbarConfig":mode="mode"/><el-card style="margin: 30px auto; padding: 0 40px; display: flex; flex-direction: column; margin-top: 150px;"><div style="margin-bottom: 10px; display: flex"><input type="text" placeholder="标题" style="height: 50px; border: none; outline: none; padding: 10px; font-size: 40px; width: fit-content;"></div><hr><Editorclass="Editor"style="height: 500px; width: 900px; margin: 0 auto; display: flex ;flex-wrap: nowrap"v-model:content="valueHtml":defaultConfig="editorConfig":mode="mode"@onCreated="handleCreated"@change="handleChange"></Editor></el-card><div style="margin-top: 20px;"><divid="editor-content-html"style="width: 80px; height: 100px; outline: none; border: 1px solid #ccc; padding: 10px; overflow-y: auto;"v-html="html"></div></div></el-form-item></template><script setup>
import '@wangeditor/editor/dist/css/style.css' // 引入编辑器样式
import { onBeforeUnmount, ref, shallowRef, onMounted } from 'vue'
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'// 编辑器实例,必须用 shallowRef
const editorRef = shallowRef(null)// 编辑器内容 HTML
const valueHtml = ref('<p>hello</p>')
const html = ref('<p>hello</p>') // 初始化内容同步到 HTML// 模拟 ajax 异步获取内容
onMounted(() => {setTimeout(() => {valueHtml.value = '<p>模拟 Ajax 异步设置内容</p>'updateHtml() // 更新 HTML 内容}, 1500)
})// 工具栏和编辑器配置
const toolbarConfig = {}
const editorConfig = { placeholder: '请输入内容...' }// 组件销毁时,及时销毁编辑器
onBeforeUnmount(() => {const editor = editorRef.valueif (editor) {editor.destroy()}
})// 编辑器创建完成时的回调函数
const handleCreated = (editor) => {editorRef.value = editor // 记录 editor 实例,重要!// 监听编辑器输入事件editor.on('change', () => {updateHtml()})
}// 更新 HTML 内容
const updateHtml = () => {if (editorRef.value) {html.value = editorRef.value.getHtml() // 实时更新 HTML 内容}
}
</script><style scoped>
.Toolbar {border-bottom: 1px solid #ccc;font-size: 15px;margin: 0 auto;
}.Editor {height: 500px;width: 900px;margin: 0 auto;
}#editor-content-html {width: 100%;height: 100px;outline: none;border: 1px solid #ccc;padding: 10px;overflow-y: auto;margin-top: 20px;
}</style>

 待更新部分

  • 图片上传
  • 悬浮框弹出ai对话框
  • 底部栏创建

已经在做的事情

ai对话聊天室功能

目前实现的功能 

特别声明:由于使用的是调用别人的接口

可能会出现服务器崩溃的问题,

能基本和ai对话,已经做了loading动画

这边是加载的时候

这边是完全显示的时候 

 

可以通过加号 选择歌曲类型进行点歌

这篇关于vue3-使用富文本编辑器-wangEditor-文章发表1的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/1050069

相关文章

使用SpringBoot+InfluxDB实现高效数据存储与查询

《使用SpringBoot+InfluxDB实现高效数据存储与查询》InfluxDB是一个开源的时间序列数据库,特别适合处理带有时间戳的监控数据、指标数据等,下面详细介绍如何在SpringBoot项目... 目录1、项目介绍2、 InfluxDB 介绍3、Spring Boot 配置 InfluxDB4、I

使用Java读取本地文件并转换为MultipartFile对象的方法

《使用Java读取本地文件并转换为MultipartFile对象的方法》在许多JavaWeb应用中,我们经常会遇到将本地文件上传至服务器或其他系统的需求,在这种场景下,MultipartFile对象非... 目录1. 基本需求2. 自定义 MultipartFile 类3. 实现代码4. 代码解析5. 自定

使用Python实现无损放大图片功能

《使用Python实现无损放大图片功能》本文介绍了如何使用Python的Pillow库进行无损图片放大,区分了JPEG和PNG格式在放大过程中的特点,并给出了示例代码,JPEG格式可能受压缩影响,需先... 目录一、什么是无损放大?二、实现方法步骤1:读取图片步骤2:无损放大图片步骤3:保存图片三、示php

使用Python实现一个简易计算器的新手指南

《使用Python实现一个简易计算器的新手指南》计算器是编程入门的经典项目,它涵盖了变量、输入输出、条件判断等核心编程概念,通过这个小项目,可以快速掌握Python的基础语法,并为后续更复杂的项目打下... 目录准备工作基础概念解析分步实现计算器第一步:获取用户输入第二步:实现基本运算第三步:显示计算结果进

python之uv使用详解

《python之uv使用详解》文章介绍uv在Ubuntu上用于Python项目管理,涵盖安装、初始化、依赖管理、运行调试及Docker应用,强调CI中使用--locked确保依赖一致性... 目录安装与更新standalonepip 安装创建php以及初始化项目依赖管理uv run直接在命令行运行pytho

C#使用Spire.XLS快速生成多表格Excel文件

《C#使用Spire.XLS快速生成多表格Excel文件》在日常开发中,我们经常需要将业务数据导出为结构清晰的Excel文件,本文将手把手教你使用Spire.XLS这个强大的.NET组件,只需几行C#... 目录一、Spire.XLS核心优势清单1.1 性能碾压:从3秒到0.5秒的质变1.2 批量操作的优雅

Kotlin 枚举类使用举例

《Kotlin枚举类使用举例》枚举类(EnumClasses)是Kotlin中用于定义固定集合值的特殊类,它表示一组命名的常量,每个枚举常量都是该类的单例实例,接下来通过本文给大家介绍Kotl... 目录一、编程枚举类核心概念二、基础语法与特性1. 基本定义2. 带参数的枚举3. 实现接口4. 内置属性三、

Java List 使用举例(从入门到精通)

《JavaList使用举例(从入门到精通)》本文系统讲解JavaList,涵盖基础概念、核心特性、常用实现(如ArrayList、LinkedList)及性能对比,介绍创建、操作、遍历方法,结合实... 目录一、List 基础概念1.1 什么是 List?1.2 List 的核心特性1.3 List 家族成

Go语言使用Gin处理路由参数和查询参数

《Go语言使用Gin处理路由参数和查询参数》在WebAPI开发中,处理路由参数(PathParameter)和查询参数(QueryParameter)是非常常见的需求,下面我们就来看看Go语言... 目录一、路由参数 vs 查询参数二、Gin 获取路由参数和查询参数三、示例代码四、运行与测试1. 测试编程路

Python使用python-pptx自动化操作和生成PPT

《Python使用python-pptx自动化操作和生成PPT》这篇文章主要为大家详细介绍了如何使用python-pptx库实现PPT自动化,并提供实用的代码示例和应用场景,感兴趣的小伙伴可以跟随小编... 目录使用python-pptx操作PPT文档安装python-pptx基础概念创建新的PPT文档查看