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

相关文章

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

全面解析HTML5中Checkbox标签

《全面解析HTML5中Checkbox标签》Checkbox是HTML5中非常重要的表单元素之一,通过合理使用其属性和样式自定义方法,可以为用户提供丰富多样的交互体验,这篇文章给大家介绍HTML5中C... 在html5中,Checkbox(复选框)是一种常用的表单元素,允许用户在一组选项中选择多个项目。本

HTML5 搜索框Search Box详解

《HTML5搜索框SearchBox详解》HTML5的搜索框是一个强大的工具,能够有效提升用户体验,通过结合自动补全功能和适当的样式,可以创建出既美观又实用的搜索界面,这篇文章给大家介绍HTML5... html5 搜索框(Search Box)详解搜索框是一个用于输入查询内容的控件,通常用于网站或应用程

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.

Python中help()和dir()函数的使用

《Python中help()和dir()函数的使用》我们经常需要查看某个对象(如模块、类、函数等)的属性和方法,Python提供了两个内置函数help()和dir(),它们可以帮助我们快速了解代... 目录1. 引言2. help() 函数2.1 作用2.2 使用方法2.3 示例(1) 查看内置函数的帮助(

Linux脚本(shell)的使用方式

《Linux脚本(shell)的使用方式》:本文主要介绍Linux脚本(shell)的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录概述语法详解数学运算表达式Shell变量变量分类环境变量Shell内部变量自定义变量:定义、赋值自定义变量:引用、修改、删

Java使用HttpClient实现图片下载与本地保存功能

《Java使用HttpClient实现图片下载与本地保存功能》在当今数字化时代,网络资源的获取与处理已成为软件开发中的常见需求,其中,图片作为网络上最常见的资源之一,其下载与保存功能在许多应用场景中都... 目录引言一、Apache HttpClient简介二、技术栈与环境准备三、实现图片下载与保存功能1.

Python中使用uv创建环境及原理举例详解

《Python中使用uv创建环境及原理举例详解》uv是Astral团队开发的高性能Python工具,整合包管理、虚拟环境、Python版本控制等功能,:本文主要介绍Python中使用uv创建环境及... 目录一、uv工具简介核心特点:二、安装uv1. 通过pip安装2. 通过脚本安装验证安装:配置镜像源(可

LiteFlow轻量级工作流引擎使用示例详解

《LiteFlow轻量级工作流引擎使用示例详解》:本文主要介绍LiteFlow是一个灵活、简洁且轻量的工作流引擎,适合用于中小型项目和微服务架构中的流程编排,本文给大家介绍LiteFlow轻量级工... 目录1. LiteFlow 主要特点2. 工作流定义方式3. LiteFlow 流程示例4. LiteF

使用Python开发一个现代化屏幕取色器

《使用Python开发一个现代化屏幕取色器》在UI设计、网页开发等场景中,颜色拾取是高频需求,:本文主要介绍如何使用Python开发一个现代化屏幕取色器,有需要的小伙伴可以参考一下... 目录一、项目概述二、核心功能解析2.1 实时颜色追踪2.2 智能颜色显示三、效果展示四、实现步骤详解4.1 环境配置4.