Skip to content

聊天消息 Chat Message

基于 Quasar 的 Chat Message↗ 组件。

参数 Param说明 Description
text消息内容(可以是一个列表的多条消息)
name消息发送者名称
label仅呈现标签标题/部分
stamp消息的时间戳
avatar头像 URL
sent是否为消息发送者(默认值:False)
text_html是否以 HTML 渲染消息(默认值:False)
python
from nicegui import ui

ui.chat_message('Hello NiceGUI!',
                name='Robot',
                stamp='now',
                avatar='https://robohash.org/ui')

ui.run()

渲染 HTML

使用 text_html 参数,可以启用 HTML 渲染功能。

python
from nicegui import ui

ui.chat_message('此段未启用 <strong>HTML</strong> 渲染')
ui.chat_message('此段启用 <strong>HTML</strong> 渲染', text_html=True)

ui.run()

换行显示

使用 \n 即可启用换行显示。

python
from nicegui import ui

ui.chat_message('This is a\nlong line!')

ui.run()

多个消息

您可以在 text 参数中传入一个列表,这样多个消息气泡就会合并在一起。

python
from nicegui import ui

ui.chat_message(['Hi! 😀', 'How are you?']
                )

ui.run()

带子元素的消息

您可以在气泡中添加子元素。

python
from nicegui import ui

with ui.chat_message():
    ui.label('Guess where I am!')
    ui.image('https://picsum.photos/id/249/640/360').classes('w-64')

ui.run()
最近更新

更新日期: 2025 年 12 月 17 日