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

news2024/10/6 14:25:26

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

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

官网地址传送 :

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

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

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

基本模版

安装依赖


npm install @wangeditor/editor --save

npm 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.value
  if (editor == null) return
  editor.destroy()
})

// 监听编辑器创建事件
const handleCreated = (editor) => {
  editorRef.value = editor // 记录 editor 实例,重要!
}

内容导入

template部分

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

<div style="margin-top: 20px;">
        <div
            id="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>

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

  <Toolbar
          class="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>
        <Editor
            class="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; ">

      <Toolbar
          class="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>
        <Editor
            class="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;">
        <div
            id="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.value
  if (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动画

这边是加载的时候

这边是完全显示的时候 

 

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

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Python的return和yield,哪个是你的菜?

目录 1、return基础介绍 &#x1f4da; 1.1 return用途&#xff1a;数据返回 1.2 return执行&#xff1a;函数终止 1.3 return深入&#xff1a;无返回值情况 2、yield核心概念 &#x1f347; 2.1 yield与迭代器 2.2 生成器函数构建 2.3 yield的暂停与续行特性 3、retur…

在 Android App 里使用 C 代码 - NDK

原生开发套件 (NDK) 是一套工具&#xff0c;使能够在 Android 应用中使用 C 和 C 代码&#xff0c;并提供众多平台库&#xff0c;可使用这些平台库管理原生 activity 和访问实体设备组件&#xff0c;例如传感器和触控输入。 NDK 可能不适合大多数 Android 编程初学者&#xff…

使用 Jetpack Compose 实现 Android 偏好设置分类界面

使用 Jetpack Compose 实现 Android 偏好设置分类界面 Jetpack Compose 提供了一种现代且声明式的构建 Android 用户界面的方法&#xff0c;使其非常适合实现偏好设置分类界面。以下是如何实现的逐步指南&#xff1a; 1. 定义数据模型: 首先&#xff0c;定义数据模型来表示…

集成学习模型对比优化—银行业务

1.Data Understanding 2.Data Exploration 3.Data Preparation 4.Training Models 5.Optimization Model 集成学习模型对比优化—银行业务 1.Data Understanding import pandas as pd from matplotlib import pyplot as plt import seaborn as sns df pd.read_csv(&quo…

表的设计与查询

目录 一、表的设计 1.第一范式&#xff08;一对一&#xff09; 定义&#xff1a; 示例&#xff1a; 2.第二范式&#xff08;一对多&#xff09; 定义&#xff1a; 要求&#xff1a; 示例&#xff1a; 3.第三范式&#xff08;多对多&#xff09; 定义&#xff1a; 要求…

Bio-Info每日一题:Rosalind-06-Counting Point Mutations

&#x1f389; 进入生物信息学的世界&#xff0c;与Rosalind一起探索吧&#xff01;&#x1f9ec; Rosalind是一个在线平台&#xff0c;专为学习和实践生物信息学而设计。该平台提供了一系列循序渐进的编程挑战&#xff0c;帮助用户从基础到高级掌握生物信息学知识。无论你是初…

每日算法——归并排序

什么是归并排序 归并排序是一种分治算法。它将数组不断地分成两半&#xff0c;对每一半进行排序&#xff0c;然后再将排序好的两半合并起来。通过不断重复这个过程&#xff0c;最终得到完全排序的数组。 归并排序的注意点&#xff1a; 空间复杂度&#xff1a;归并排序需要额…

javascript动态绑定

介绍 先来看看ai的解释 动态绑定机制是面向对象编程中的一个核心概念&#xff0c;特别是在Java这样的语言中。它允许在运行时根据对象的实际类型来决定调用哪个方法&#xff0c;而不是在编译时。这是多态性的关键特性之一。 在Java中&#xff0c;动态绑定是通过方法调用和方法…

C#——枚举类型详情

枚举类型 枚举类型&#xff08;也可以称为“枚举器”&#xff09;由一组具有独立标识符&#xff08;名称&#xff09;的整数类型常量构成&#xff0c;在 C# 中枚举类型不仅可以在类或结构体的内部声明&#xff0c;也可以在类或结构体的外部声明&#xff0c;默认情况下枚举类型…

ViT:2 理解CLIP

大模型技术论文不断&#xff0c;每个月总会新增上千篇。本专栏精选论文重点解读&#xff0c;主题还是围绕着行业实践和工程量产。若在某个环节出现卡点&#xff0c;可以回到大模型必备腔调或者LLM背后的基础模型新阅读。而最新科技&#xff08;Mamba,xLSTM,KAN&#xff09;则提…

大模型基础——从零实现一个Transformer(2)

大模型基础——从零实现一个Transformer(1) 一、引言 上一章主要实现了一下Transformer里面的BPE算法和 Embedding模块定义 本章主要讲一下 Transformer里面的位置编码以及多头注意力 二、位置编码 2.1正弦位置编码(Sinusoidal Position Encoding) 其中&#xff1a; pos&…

linux中xterm窗口怎么调整字体大小

需求&#xff1a;打开的xterm窗口字体比较小&#xff0c;怎么才能调整字体大小&#xff0c;打开的大写&#xff1a; 解决方法&#xff1a; 在home目录下搞一个设置文件 .Xresource&#xff0c;里面内容如下 然后把设置文件添加到 .tcshrc 文件中生效 这样重新打开的xterm字…

MySQL数据库(二)和java复习

一.MySQL数据库学习(二) (一).DQL查询数据 DQL&#xff08;Data Query Language&#xff09;是用于从数据库中检索数据的语言。常见的 DQL 语句包括 SELECT、FROM、WHERE、GROUP BY、HAVING 和 ORDER BY 等关键字&#xff0c;用于指定要检索的数据、数据源、过滤条件、分组方…

《编程小白变大神:DjangoBlog带你飞越代码海洋》

还在为你的博客加载速度慢而烦恼&#xff1f;DjangoBlog性能优化大揭秘&#xff0c;让你的网站速度飞跃提升&#xff01;本文将带你深入了解缓存策略、数据库优化、静态文件处理等关键技术&#xff0c;更有Gunicorn和Nginx的黄金搭档&#xff0c;让你的博客部署如虎添翼。无论你…

助力高考,一组彩色的文字

1、获取文本内容 首先&#xff0c;获取每个<div>元素的文本内容&#xff0c;并清空其内部HTML&#xff08;innerHTML ""&#xff09;。 2、创建<span>元素 然后&#xff0c;它遍历文本的每个字符&#xff0c;为每个字符创建一个新的<span>元素…

《python程序语言设计》2018版第5章第36题改造4.17 石头 剪刀 布某一方超过2次就结束。

代码编写记录 2024.05.04 05.36.01version 换一个什么数代替剪子 我先建立一个函数judgement condition 石头3 剪子2 布1 如何构建一个循环进行的架构&#xff0c;是我们最需要的想法 循环以什么条件开始呢 是小于2个还是大于2个。 guess_num random.randint(1, 3) computer…

nginx优化与防盗链【☆☆☆】

目录 一、用户层面的优化 1、隐藏版本号 方法一&#xff1a;修改配置文件 方法二&#xff1a;修改源码文件&#xff0c;重新编译安装 2、修改nginx用户与组 3、配置nginx网页缓存时间 4、nginx的日志切割 5、配置nginx实现连接超时 6、更改nginx运行进程数 7、开启网…

IPv4 子网掩码计算器—python代码实现

今天聊一下&#xff0c;我用python和vscode工具实现一个IPv4计算器的一些思路&#xff0c;以及使用Python编写IPv4计算器一些好处&#xff1f; 首先&#xff0c;一、Python语法简洁易读&#xff0c;便于理解和维护&#xff0c;即使对编程不熟悉的用户也能快速了解代码逻辑。其…

阿里通义千问 Qwen2 大模型开源发布

阿里通义千问 Qwen2 大模型开源发布 Qwen2 系列模型是 Qwen1.5 系列模型的重大升级。该系列包括了五个不同尺寸的预训练和指令微调模型&#xff1a;Qwen2-0.5B、Qwen2-1.5B、Qwen2-7B、Qwen2-57B-A14B 以及 Qwen2-72B。 在中文和英文的基础上&#xff0c;Qwen2 系列的训练数…

已解决Error || RuntimeError: size mismatch, m1: [32 x 100], m2: [500 x 10]

已解决Error || RuntimeError: size mismatch, m1: [32 x 100], m2: [500 x 10] 原创作者&#xff1a; 猫头虎 作者微信号&#xff1a; Libin9iOak 作者公众号&#xff1a; 猫头虎技术团队 更新日期&#xff1a; 2024年6月6日 博主猫头虎的技术世界 &#x1f31f; 欢迎来…