使用自动导入的 useToast composable 来显示 Toast 通知。
<script setup lang="ts">
const toast = useToast()
</script>
useToast composable 使用 Nuxt 的 useState 来管理吐司状态,确保应用程序中的响应性。useToast()
useToast composable 提供全局管理吐司通知的方法。
add(toast: Partial<Toast>): Toast
添加新的吐司通知。
Toast 对象true。Toast 接口中的其他属性。返回: 添加的完整 Toast 对象。
<script setup lang="ts">
const toast = useToast()
function showToast() {
toast.add({
title: 'Success',
description: 'Your action was completed successfully.',
color: 'success'
})
}
</script>
update(id: string | number, toast: Partial<Toast>): void
更新现有的吐司通知。
Toast 对象。<script setup lang="ts">
const toast = useToast()
function updateToast(id: string | number) {
toast.update(id, {
title: 'Updated Toast',
description: 'This toast has been updated.'
})
}
</script>
remove(id: string | number): void
移除吐司通知。
<script setup lang="ts">
const toast = useToast()
function removeToast(id: string | number) {
toast.remove(id)
}
</script>
clear(): void
移除所有吐司通知。
<script setup lang="ts">
const toast = useToast()
function clearAllToasts() {
toast.clear()
}
</script>
toaststoasts: Ref<Toast[]>
Ref<Toast[]>