当前位置: 首页 > article >正文

聊天组件 Vue3-beautiful-chat

前言

最近很多公司都在搞大模型,类似于 chatgpt 的功能;而 chatgpt 的界面其实就是个对话框。今天就介绍一个不错的对话框组件 Vue3-beautiful-chat

项目框架

vite + vue3 + TS + Vue3-beautiful-chat

使用流程

1、引用三方件

npm install Vue3-beautiful-chat

2、在 main.ts 中添加依赖

import Chat from 'vue3-beautiful-chat'

app.use(Chat)

3、创建 chatView.vue 组件

<template>
  <div>
    <beautiful-chat
      :participants="localVars.participants"
      :titleImageUrl="localVars.titleImageUrl"
      :onMessageWasSent="onMessageWasSent"
      :messageList="localVars.messageList"
      :newMessagesCount="localVars.newMessagesCount"
      :isOpen="localVars.isChatOpen"
      :close="closeChat"
      :icons="icons"
      :open="openChat"
      :showEmoji="true"
      :showFile="true"
      :showEdition="true"
      :showDeletion="true"
      :deletionConfirmation="true"
      :showTypingIndicator="localVars.showTypingIndicator"
      :showLauncher="true"
      :showCloseButton="true"
      :colors="localVars.colors"
      :alwaysScrollToBottom="localVars.alwaysScrollToBottom"
      :disableUserListToggle="false"
      :messageStyling="localVars.messageStyling"
      @onType="handleOnType"
      @edit="editMessage" />
  </div>
</template>
<script setup lang='ts'>
import { reactive } from 'vue'

const localVars = reactive({
    participants: [
      {
        id: 'user1',
        name: 'Matteo',
        imageUrl: 'https://avatars3.githubusercontent.com/u/1915989?s=230&v=4'
      },
      {
        id: 'user2',
        name: 'Support',
        imageUrl: 'https://avatars3.githubusercontent.com/u/37018832?s=200&v=4'
      }
    ], // the list of all the participant of the conversation. `name` is the user name, `id` is used to establish the author of a message, `imageUrl` is supposed to be the user avatar.
    titleImageUrl: 'https://a.slack-edge.com/66f9/img/avatars-teams/ava_0001-34.png',
    messageList: [
        { type: 'text', author: `me`, data: { text: `Say yes!` } },
        { type: 'text', author: `user1`, data: { text: `No.` } }
    ], // the list of the messages to show, can be paginated and adjusted dynamically
    newMessagesCount: 0,
    isChatOpen: false, // to determine whether the chat window should be open or closed
    showTypingIndicator: '', // when set to a value matching the participant.id it shows the typing indicator for the specific user
    colors: {
      header: {
        bg: '#4e8cff',
        text: '#ffffff'
      },
      launcher: {
        bg: '#4e8cff'
      },
      messageList: {
        bg: '#ffffff'
      },
      sentMessage: {
        bg: '#4e8cff',
        text: '#ffffff'
      },
      receivedMessage: {
        bg: '#eaeaea',
        text: '#222222'
      },
      userInput: {
        bg: '#f4f7f9',
        text: '#565867'
      }
    }, // specifies the color scheme for the component
    alwaysScrollToBottom: false, // when set to true always scrolls the chat to the bottom when new events are in (new message, user starts typing...)
    messageStyling: true // enables *bold* /emph/ _underline_ and such (more info at github.com/mattezza/msgdown)
})


const sendMessage = (text) => {
    if (text.length > 0) {
        localVars.newMessagesCount = localVars.isChatOpen ? localVars.newMessagesCount : localVars.newMessagesCount + 1
        onMessageWasSent({ author: 'support', type: 'text', data: { text } })
    }
}
const onMessageWasSent = (message) => {
      // called when the user sends a message
    localVars.messageList = [ ...localVars.messageList, newMessages ]
}
const openChat = () => {
      // called when the user clicks on the fab button to open the chat
    localVars.isChatOpen = true
    localVars.newMessagesCount = 0
}
const closeChat = () => {
    // called when the user clicks on the botton to close the chat
    localVars.isChatOpen = false
}
const handleScrollToTop = () => {
    // called when the user scrolls message list to top
    // leverage pagination for loading another page of messages
}
const handleOnType = () => {
    console.log('Emit typing event')
}
const editMessage = (message) => {
    const m = localVars.messageList.find(m=>m.id === message.id);
    m.isEdited = true;
    m.data.text = message.data.text;
}

</script>

4、当我们启动本地项目的时候,可以看到页面上会出现个图标
在这里插入图片描述
点击这个图标的时候就会出现对话框,我们就可以进行正常的对话操作了。

实际项目使用

1、我们在实际项目中最好是将这个 chatView.vue 作为一个组件在其他页面进行引用。
import ChatView from '/chatView.vue'
2、如果嫌弃这个 chat 图标太丑,我们也可以进行更换
组件上有一个 icons 属性,这个可以设置 open 和 close 图标

icons: {
    open: {
      img: string
    }
    close: {
      img: string
    }
  }

3、大家在点击 header 的时候会出现一个用户弹窗,这个实际用得比较少,而且还影响美观;这个功能也是可以关闭的 disableUserListToggle 属性改成 true
:disableUserListToggle="true"


http://www.kler.cn/news/305929.html

相关文章:

  • 【QT】实现TCP服务器,客户端之间的通信
  • 国风编曲:了解国风 民族调式 五声音阶 作/编曲思路 变化音 六声、七声调式
  • 【开源免费】基于SpringBoot+Vue.JS在线旅游网站(JAVA毕业设计)
  • 威胁建模网络与云威胁
  • SQL进阶的技巧:如何实现某列的累计乘积?
  • Codeforces Round 921 (Div. 2) A~D
  • 英飞凌MCU第五代高性能CAPSENSE技术PSoC4000T
  • Leetcode 二叉树中根遍历
  • 力扣-96.不同的二叉搜索树 题目详解
  • Android Radio2.0——动态列表回调(七)
  • tcp、http和rpc
  • WebSocket详细介绍
  • OPEN AI o1已经像人类一样思考了。。。
  • 【iOS】present和push
  • 【AcWing】快速排序的Go实现
  • yolo训练出现Could not load library libcudnn_cnn_train.so.8问题及解决方法
  • 从大脑图谱/ROI中提取BOLD信号
  • 简单易懂的方式来解释机器学习(ML)和深度学习(DL)的区别与联系
  • 通信工程学习:什么是DWDM密集波分复用
  • 小众语言ruby在苹果中的初步应用
  • self-play RL学习笔记
  • 【开源免费】基于SpringBoot+Vue.JS购物商城网站(JAVA毕业设计)
  • ImDisk Toolkit将一部分RAM模拟成硬盘分区
  • 更新20240915机器视觉海康Visionmaster学习步骤
  • 解决tiktoken库调用get_encoding时SSL超时
  • Redis 与数据库数据一致性保证详解
  • MySQL——数据库的高级操作(二)用户管理(5)如何解决 root 用户密码丢失
  • 【QT】自制一个简单的时钟(跟随系统时间)
  • 9.15javaweb项目总结
  • vs code: pnpm : 无法加载文件 C:\Program Files\nodejs\pnpm.ps1,因为在此系统上禁止运行脚本