用法
ChatPrompt 组件会渲染一个 <form>
元素,并扩展了 Textarea 组件,以便您可以传入任何属性,例如 icon
、placeholder
、autofocus
等。
- 当用户按下 ↵ 键或点击提交按钮时,表单会被提交。
- 当 ⎋ 键被按下时,文本区域会失去焦点,并发出一个
close
事件。
变体
使用 variant
属性来改变提示的样式。默认为 outline
。
<template>
<UChatPrompt variant="soft" />
</template>
示例
在页面内
将 ChatPrompt 组件与 useChat
组合式函数结合使用,以在页面中显示聊天提示。
传入 input
属性以及 error
属性,以在发生错误时禁用文本区域。
<script setup lang="ts">
import { useChat } from '@ai-sdk/vue'
const { messages, input, handleSubmit, reload, stop, status, error } = useChat()
</script>
<template>
<UDashboardPanel>
<template #body>
<UContainer>
<UChatMessages :messages="messages" :status="status">
<template #content="{ message }">
<MDC :value="message.content" :cache-key="message.id" unwrap="p" />
</template>
</UChatMessages>
</UContainer>
</template>
<template #footer>
<UContainer>
<UChatPrompt v-model="input" :error="error" @submit="handleSubmit">
<UChatPromptSubmit :status="status" @stop="stop" @reload="reload" />
</UChatPrompt>
</UContainer>
</template>
</UDashboardPanel>
</template>
您也可以将其用作聊天界面的起点。
<script setup lang="ts">
const input = ref('')
const loading = ref(false)
async function onSubmit() {
loading.value = true
const chat = await $fetch('/api/chats', {
method: 'POST',
body: { input }
})
navigateTo(`/chat/${chat.id}`)
}
</script>
<template>
<UDashboardPanel>
<template #body>
<UContainer>
<h1>How can I help you today?</h1>
<UChatPrompt v-model="input" :status="loading ? 'streaming' : 'ready'" @submit="onSubmit">
<UChatPromptSubmit />
</UChatPrompt>
</UContainer>
</template>
</UDashboardPanel>
</template>
API
属性
属性 | 默认值 | 类型 |
---|---|---|
as |
|
此组件应渲染为的元素或组件。 |
placeholder |
|
这是文本区域的占位符文本。 |
autofocus |
|
|
autoresize |
|
|
rows |
|
|
variant |
|
|
error |
| |
图标 |
根据 | |
loading |
当为 | |
loadingIcon |
|
当 |
avatar |
在左侧显示头像。
| |
autofocusDelay |
| |
autoresizeDelay |
| |
maxrows |
| |
modelValue |
| |
ui |
|
插槽
插槽 | 类型 |
---|---|
页头 |
|
页脚 |
|
前置 |
|
default |
|
尾部 |
|
事件
事件 | 类型 |
---|---|
close |
|
submit |
|
update:modelValue |
|
主题
export default defineAppConfig({
uiPro: {
chatPrompt: {
slots: {
root: 'relative flex flex-col items-stretch gap-2 px-2.5 py-2 w-full rounded-lg backdrop-blur',
header: 'flex items-center gap-1.5',
body: 'items-start',
footer: 'flex items-center justify-between gap-1.5',
base: 'text-base/5'
},
variants: {
variant: {
outline: {
root: 'bg-default/75 ring ring-default'
},
soft: {
root: 'bg-elevated/50'
},
subtle: {
root: 'bg-elevated/50 ring ring-default'
},
naked: {
root: ''
}
}
},
defaultVariants: {
variant: 'outline'
}
}
}
})
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import ui from '@nuxt/ui/vite'
export default defineConfig({
plugins: [
vue(),
ui({
uiPro: {
chatPrompt: {
slots: {
root: 'relative flex flex-col items-stretch gap-2 px-2.5 py-2 w-full rounded-lg backdrop-blur',
header: 'flex items-center gap-1.5',
body: 'items-start',
footer: 'flex items-center justify-between gap-1.5',
base: 'text-base/5'
},
variants: {
variant: {
outline: {
root: 'bg-default/75 ring ring-default'
},
soft: {
root: 'bg-elevated/50'
},
subtle: {
root: 'bg-elevated/50 ring ring-default'
},
naked: {
root: ''
}
}
},
defaultVariants: {
variant: 'outline'
}
}
}
})
]
})
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import uiPro from '@nuxt/ui-pro/vite'
export default defineConfig({
plugins: [
vue(),
uiPro({
uiPro: {
chatPrompt: {
slots: {
root: 'relative flex flex-col items-stretch gap-2 px-2.5 py-2 w-full rounded-lg backdrop-blur',
header: 'flex items-center gap-1.5',
body: 'items-start',
footer: 'flex items-center justify-between gap-1.5',
base: 'text-base/5'
},
variants: {
variant: {
outline: {
root: 'bg-default/75 ring ring-default'
},
soft: {
root: 'bg-elevated/50'
},
subtle: {
root: 'bg-elevated/50 ring ring-default'
},
naked: {
root: ''
}
}
},
defaultVariants: {
variant: 'outline'
}
}
}
})
]
})