Skip to content

ConversationList 对话列表 ​

对话历史列表容器组件,支持新消息平滑自动滚动到底部、到顶触发加载更早历史消息,以及前插历史消息时的视口位置锚定补偿。

基础用法 ​

你好,我想学习 Vue 3
01:56
你好!很高兴你想学习 Vue 3。它的核心特性包括组合式 API、更好的 TypeScript 支持以及细粒度响应式性能提升。
01:57
能给我一个对话列表的示例吗?
01:58
当然可以!ConversationList 会自动配合 ChatMessage 处理流式滚动和历史加载锚点补偿。
01:58
<script setup lang="ts">
import { ref } from 'vue'
import { ConversationList, ChatMessage } from '@bobocn/element/vue'

const messages = ref([
  { id: 1, role: 'user' as const, content: '你好,我想学习 Vue 3', timestamp: new Date(Date.now() - 120000) },
  {
    id: 2,
    role: 'assistant' as const,
    content: '你好!很高兴你想学习 Vue 3。它的核心特性包括组合式 API、更好的 TypeScript 支持以及细粒度响应式性能提升。',
    timestamp: new Date(Date.now() - 60000)
  },
  { id: 3, role: 'user' as const, content: '能给我一个对话列表的示例吗?', timestamp: new Date() },
  {
    id: 4,
    role: 'assistant' as const,
    content: '当然可以!ConversationList 会自动配合 ChatMessage 处理流式滚动和历史加载锚点补偿。',
    timestamp: new Date()
  }
])

function appendReply() {
  const nextId = messages.value.length + 1
  messages.value.push({
    id: nextId,
    role: nextId % 2 === 1 ? 'user' : 'assistant',
    content: nextId % 2 === 1 ? '继续补充一个问题' : '这是新追加的回复,列表会自动平滑滚到底部。',
    timestamp: new Date()
  })
}
</script>

<template>
  <div style="display: flex; flex-direction: column; gap: 10px;">
    <div style="border: 1px solid var(--vk-border-color-light); border-radius: 12px; overflow: hidden;">
      <ConversationList height="320px">
        <ChatMessage
          v-for="msg in messages"
          :key="msg.id"
          :role="msg.role"
          :timestamp="msg.timestamp"
        >
          {{ msg.content }}
        </ChatMessage>
      </ConversationList>
    </div>
    <div style="display: flex; justify-content: flex-end;">
      <button
        type="button"
        style="padding: 5px 12px; font-size: 12.5px; border-radius: 6px; border: 1px solid var(--vk-border-color); background: var(--vk-fill-color-blank); cursor: pointer;"
        @click="appendReply"
      >
        追加一条消息
      </button>
    </div>
  </div>
</template>

加载更多 ​

滚动到顶部时触发 load-more 事件。内置 preserveScrollOnPrepend 机制,能在历史消息前插入列表后自动补偿滚动高度,防止页面视野突跳。

历史消息 1:如何排查接口超时问题?
01:52
历史消息 2:建议先核对网关超时配置与慢查询日志。
01:53
历史消息 3:已经定位到是缓存穿透引起的。
01:54
历史消息 4:可以补充布隆过滤器或空值短缓存。
01:55
向上滚动到顶部即可触发加载更多历史记录。
01:56
前插历史消息时会自动保持当前阅读视口位置不跳动。
01:57
<script setup lang="ts">
import { ref } from 'vue'
import { ConversationList, ChatMessage } from '@bobocn/element/vue'

let nextId = 1
const loading = ref(false)
const messages = ref([
  { id: nextId++, role: 'user' as const, content: '历史消息 1:如何排查接口超时问题?', timestamp: new Date(Date.now() - 360000) },
  { id: nextId++, role: 'assistant' as const, content: '历史消息 2:建议先核对网关超时配置与慢查询日志。', timestamp: new Date(Date.now() - 300000) },
  { id: nextId++, role: 'user' as const, content: '历史消息 3:已经定位到是缓存穿透引起的。', timestamp: new Date(Date.now() - 240000) },
  { id: nextId++, role: 'assistant' as const, content: '历史消息 4:可以补充布隆过滤器或空值短缓存。', timestamp: new Date(Date.now() - 180000) },
  { id: nextId++, role: 'user' as const, content: '向上滚动到顶部即可触发加载更多历史记录。', timestamp: new Date(Date.now() - 120000) },
  { id: nextId++, role: 'assistant' as const, content: '前插历史消息时会自动保持当前阅读视口位置不跳动。', timestamp: new Date(Date.now() - 60000) }
])

function handleLoadMore() {
  if (loading.value) return
  loading.value = true
  setTimeout(() => {
    const older = [
      { id: nextId++, role: 'user' as const, content: '更早的用户提问:系统架构演进背景。', timestamp: new Date(Date.now() - 480000) },
      { id: nextId++, role: 'assistant' as const, content: '更早的助手回复:历史记录已平滑插入顶部。', timestamp: new Date(Date.now() - 420000) }
    ]
    messages.value = [...older, ...messages.value]
    loading.value = false
  }, 800)
}
</script>

<template>
  <div style="border: 1px solid var(--vk-border-color-light); border-radius: 12px; overflow: hidden;">
    <ConversationList height="280px" :loading-more="loading" @load-more="handleLoadMore">
      <ChatMessage
        v-for="message in messages"
        :key="message.id"
        :role="message.role"
        :timestamp="message.timestamp"
      >
        {{ message.content }}
      </ChatMessage>
    </ConversationList>
  </div>
</template>

双框架使用示例 ​

vue
<template>
  <ConversationList
    height="480px"
    :loading-more="loading"
    loading-text="正在拉取更早的对话记录..."
    :preserve-scroll-on-prepend="true"
    @load-more="handleLoadMore"
  >
    <ChatMessage
      v-for="msg in messages"
      :key="msg.id"
      :role="msg.role"
      :timestamp="msg.timestamp"
    >
      {{ msg.content }}
    </ChatMessage>
  </ConversationList>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { ConversationList, ChatMessage } from '@bobocn/element/vue'
import '@bobocn/element/style.css'

const loading = ref(false)
const messages = ref([
  { id: 1, role: 'user' as const, content: '你好,请帮我分析这份日志', timestamp: new Date() },
  { id: 2, role: 'assistant' as const, content: '请提供日志内容,我将为您分析排查。', timestamp: new Date() }
])

const handleLoadMore = () => {
  if (loading.value) return
  loading.value = true
  setTimeout(() => {
    // 模拟向前插入 2 条历史记录
    messages.value.unshift(
      { id: -2, role: 'user' as const, content: '之前的会话 1', timestamp: new Date(Date.now() - 3600000) },
      { id: -1, role: 'assistant' as const, content: '之前的回复 1', timestamp: new Date(Date.now() - 3500000) }
    )
    loading.value = false
  }, 1000)
}
</script>
tsx
import React, { useState } from 'react'
import { ConversationList, ChatMessage } from '@bobocn/element/react'
import '@bobocn/element/style.css'

export default function App() {
  const [loading, setLoading] = useState(false)
  const [messages, setMessages] = useState([
    { id: 1, role: 'user' as const, content: '你好,请帮我分析这份日志', timestamp: new Date() },
    { id: 2, role: 'assistant' as const, content: '请提供日志内容,我将为您分析排查。', timestamp: new Date() }
  ])

  const handleLoadMore = () => {
    if (loading) return
    setLoading(true)
    setTimeout(() => {
      setMessages(prev => [
        { id: -2, role: 'user' as const, content: '之前的会话 1', timestamp: new Date(Date.now() - 3600000) },
        { id: -1, role: 'assistant' as const, content: '之前的回复 1', timestamp: new Date(Date.now() - 3500000) },
        ...prev
      ])
      setLoading(false)
    }, 1000)
  }

  return (
    <ConversationList
      height="480px"
      loadingMore={loading}
      loadingText="正在拉取更早的对话记录..."
      preserveScrollOnPrepend
      onLoadMore={handleLoadMore}
    >
      {messages.map(msg => (
        <ChatMessage key={msg.id} role={msg.role} timestamp={msg.timestamp}>
          {msg.content}
        </ChatMessage>
      ))}
    </ConversationList>
  )
}

API ​

属性 (Attributes / Props) ​

属性名说明类型默认值
autoScroll新消息追加时是否自动平滑滚到底部booleantrue
loadingMore是否正在向上加载历史消息booleanfalse
loadMoreThreshold触发顶部加载更多的滚动距离阈值(px)number16
height容器固定高度(支持 CSS 单位如 '480px' / '60vh')string—
maxHeight容器最大高度string'100%'
preserveScrollOnPrepend向上加载历史消息前插时保持当前视口位置(滚动补偿)booleantrue
showScrollToBottom上滑脱离底部时是否显示“回到底部”悬浮按钮booleantrue
scrollToBottomText回到底部悬浮按钮文案string'回到底部'
emptyText无消息时的空状态文案string'暂无对话'
loadingText向上加载历史消息时的提示文本string'加载中...'

事件 (Events) ​

事件名说明回调参数
load-more / onLoadMore滚动到顶部阈值内时触发加载更多—
scroll / onScroll列表滚动时触发(scrollTop: number)
at-bottom-change / onAtBottomChange列表是否处于底部状态变化时触发(atBottom: boolean)

方法 (Exposes / Ref) ​

方法名说明类型
scrollToBottom手动平滑滚动到列表最底部() => void
getElement获取滚动容器原生 DOM 节点() => HTMLElement | null

插槽 / Children ​

插槽名说明
default消息元素容器(通常为 ChatMessage 集合)
empty自定义空状态内容(如组合 Welcome 与 Prompts)

基于 MIT 许可发布