主题
useConfirmDialog
创建事件钩子以支持模式和确认对话框链。
🌐 Creates event hooks to support modals and confirmation dialog chains.
可以在模板上使用函数,而钩子是模态对话框或其他需要用户确认的操作的业务逻辑的便捷骨架。
🌐 Functions can be used on the template, and hooks are a handy skeleton for the business logic of modals dialog or other actions that require user confirmation.
示例
函数和钩子
🌐 Functions and hooks
reveal()- 触发onReveal钩子并将revealed.value设置为true。返回一个在confirm()或cancel()时解决的 promise。confirm()- 将isRevealed.value设置为false并触发onConfirm钩子。cancel()- 将isRevealed.value设置为false并触发onCancel钩子。
基本用法
🌐 Basic Usage
使用钩子
🌐 Using hooks
vue
<script setup lang="ts">
import { useConfirmDialog } from '@vueuse/core'
const { isRevealed, reveal, confirm, cancel, onReveal, onConfirm, onCancel }
= useConfirmDialog()
</script>
<template>
<button @click="reveal">
Reveal Modal
</button>
<teleport to="body">
<div v-if="isRevealed" class="modal-bg">
<div class="modal">
<h2>Confirm?</h2>
<button @click="confirm">
Yes
</button>
<button @click="cancel">
Cancel
</button>
</div>
</div>
</teleport>
</template>承诺
🌐 Promise
如果你更喜欢使用 Promise:
🌐 If you prefer working with promises:
vue
<script setup lang="ts">
import { useConfirmDialog } from '@vueuse/core'
const {
isRevealed,
reveal,
confirm,
cancel,
} = useConfirmDialog()
async function openDialog() {
const { data, isCanceled } = await reveal()
if (!isCanceled)
console.log(data)
}
</script>
<template>
<button @click="openDialog">
Show Modal
</button>
<teleport to="body">
<div v-if="isRevealed" class="modal-layout">
<div class="modal">
<h2>Confirm?</h2>
<button @click="confirm(true)">
Yes
</button>
<button @click="confirm(false)">
No
</button>
</div>
</div>
</teleport>
</template>