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

相关文章

Java学习手册之Filter和Listener使用方法

《Java学习手册之Filter和Listener使用方法》:本文主要介绍Java学习手册之Filter和Listener使用方法的相关资料,Filter是一种拦截器,可以在请求到达Servl... 目录一、Filter(过滤器)1. Filter 的工作原理2. Filter 的配置与使用二、Listen

Pandas使用AdaBoost进行分类的实现

《Pandas使用AdaBoost进行分类的实现》Pandas和AdaBoost分类算法,可以高效地进行数据预处理和分类任务,本文主要介绍了Pandas使用AdaBoost进行分类的实现,具有一定的参... 目录什么是 AdaBoost?使用 AdaBoost 的步骤安装必要的库步骤一:数据准备步骤二:模型

使用Pandas进行均值填充的实现

《使用Pandas进行均值填充的实现》缺失数据(NaN值)是一个常见的问题,我们可以通过多种方法来处理缺失数据,其中一种常用的方法是均值填充,本文主要介绍了使用Pandas进行均值填充的实现,感兴趣的... 目录什么是均值填充?为什么选择均值填充?均值填充的步骤实际代码示例总结在数据分析和处理过程中,缺失数

如何使用 Python 读取 Excel 数据

《如何使用Python读取Excel数据》:本文主要介绍使用Python读取Excel数据的详细教程,通过pandas和openpyxl,你可以轻松读取Excel文件,并进行各种数据处理操... 目录使用 python 读取 Excel 数据的详细教程1. 安装必要的依赖2. 读取 Excel 文件3. 读

解决Maven项目idea找不到本地仓库jar包问题以及使用mvn install:install-file

《解决Maven项目idea找不到本地仓库jar包问题以及使用mvninstall:install-file》:本文主要介绍解决Maven项目idea找不到本地仓库jar包问题以及使用mvnin... 目录Maven项目idea找不到本地仓库jar包以及使用mvn install:install-file基

Python使用getopt处理命令行参数示例解析(最佳实践)

《Python使用getopt处理命令行参数示例解析(最佳实践)》getopt模块是Python标准库中一个简单但强大的命令行参数处理工具,它特别适合那些需要快速实现基本命令行参数解析的场景,或者需要... 目录为什么需要处理命令行参数?getopt模块基础实际应用示例与其他参数处理方式的比较常见问http

C 语言中enum枚举的定义和使用小结

《C语言中enum枚举的定义和使用小结》在C语言里,enum(枚举)是一种用户自定义的数据类型,它能够让你创建一组具名的整数常量,下面我会从定义、使用、特性等方面详细介绍enum,感兴趣的朋友一起看... 目录1、引言2、基本定义3、定义枚举变量4、自定义枚举常量的值5、枚举与switch语句结合使用6、枚

使用Python从PPT文档中提取图片和图片信息(如坐标、宽度和高度等)

《使用Python从PPT文档中提取图片和图片信息(如坐标、宽度和高度等)》PPT是一种高效的信息展示工具,广泛应用于教育、商务和设计等多个领域,PPT文档中常常包含丰富的图片内容,这些图片不仅提升了... 目录一、引言二、环境与工具三、python 提取PPT背景图片3.1 提取幻灯片背景图片3.2 提取

使用Python实现图像LBP特征提取的操作方法

《使用Python实现图像LBP特征提取的操作方法》LBP特征叫做局部二值模式,常用于纹理特征提取,并在纹理分类中具有较强的区分能力,本文给大家介绍了如何使用Python实现图像LBP特征提取的操作方... 目录一、LBP特征介绍二、LBP特征描述三、一些改进版本的LBP1.圆形LBP算子2.旋转不变的LB

Maven的使用和配置国内源的保姆级教程

《Maven的使用和配置国内源的保姆级教程》Maven是⼀个项目管理工具,基于POM(ProjectObjectModel,项目对象模型)的概念,Maven可以通过一小段描述信息来管理项目的构建,报告... 目录1. 什么是Maven?2.创建⼀个Maven项目3.Maven 核心功能4.使用Maven H