Skip to content

useAsyncConfirm

统一的异步确认钩子。

参数

名称类型描述
confirmServicefunction异步确认服务。
optionsAsyncConfirmOptions确认选项。

AsyncConfirmOptions

名称类型默认值描述
titlestringConfirm确认对话框的标题。
messagestringSure Confirm确认对话框的消息。
typeMessageTypeerror确认对话框的类型("primary"、"success"、"info"、"warning"、"error")。
confirmButtonTextstringSure确认按钮的文本。
cancelButtonTextstringCancel取消按钮的文本。
successMessagestringundefined异步确认成功后显示的成功消息。
errorMessagestringundefined异步确认失败后显示的错误消息。
confirmSuccess() => voidundefined确认成功时的回调函数。
confirmError(error: unknown) => voidundefined确认失败时的回调函数。

返回值

名称类型描述
confirmconfirm(params?: unknown)确认函数。params 会传递给 confirmService。
loadingRef<boolean>加载状态

基础用法

vue
<script setup lang="ts">
import { useAsyncConfirm } from '@vuecraft/components'
import axios from 'axios'

async function deleteItem() {
  return await axios.delete('/api/items/1')
}

const { confirm, loading } = useAsyncConfirm(deleteItem, {
  title: 'Delete Item',
  message: 'Are you sure you want to delete this item?',
  type: 'error',
  successMessage: 'Deleted successfully',
})
</script>

<template>
  <button :loading="loading" @click="confirm()">
    {{ loading ? 'Loading...' : 'Delete' }}
  </button>
</template>

基于 MIT 许可证发布。