mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-03-17 01:20:46 +08:00
fix: 弹出弹窗逻辑修改
This commit is contained in:
@@ -1,42 +0,0 @@
|
||||
import { ref } from 'vue'
|
||||
|
||||
const state = ref({
|
||||
visible: false,
|
||||
title: '',
|
||||
message: '',
|
||||
resolve: null,
|
||||
reject: null,
|
||||
})
|
||||
|
||||
export const useConfirm = () => {
|
||||
const confirm = (title, message) => {
|
||||
state.value.title = title
|
||||
state.value.message = message
|
||||
state.value.visible = true
|
||||
return new Promise((resolve, reject) => {
|
||||
state.value.resolve = resolve
|
||||
state.value.reject = reject
|
||||
})
|
||||
}
|
||||
|
||||
const onConfirm = () => {
|
||||
if (state.value.resolve) {
|
||||
state.value.resolve(true)
|
||||
}
|
||||
state.value.visible = false
|
||||
}
|
||||
|
||||
const onCancel = () => {
|
||||
if (state.value.reject) {
|
||||
state.value.reject(false)
|
||||
}
|
||||
state.value.visible = false
|
||||
}
|
||||
|
||||
return {
|
||||
confirm,
|
||||
onConfirm,
|
||||
onCancel,
|
||||
state,
|
||||
}
|
||||
}
|
||||
52
frontend_nuxt/composables/useConfirm.ts
Normal file
52
frontend_nuxt/composables/useConfirm.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
// composables/useConfirm.ts
|
||||
import { ref } from 'vue'
|
||||
|
||||
// 全局单例(SPA 下即可;Nuxt/SSR 下见文末“SSR 提醒”)
|
||||
const visible = ref(false)
|
||||
const title = ref('')
|
||||
const message = ref('')
|
||||
|
||||
let resolver: ((ok: boolean) => void) | null = null
|
||||
|
||||
function reset() {
|
||||
visible.value = false
|
||||
title.value = ''
|
||||
message.value = ''
|
||||
resolver = null
|
||||
}
|
||||
|
||||
export function useConfirm() {
|
||||
/**
|
||||
* 打开确认框,返回 Promise<boolean>
|
||||
* - 确认 => resolve(true)
|
||||
* - 取消/关闭 => resolve(false)
|
||||
* 若并发调用,以最后一次为准(更简单直观)
|
||||
*/
|
||||
const confirm = (t: string, m: string) => {
|
||||
title.value = t
|
||||
message.value = m
|
||||
visible.value = true
|
||||
return new Promise<boolean>((resolve) => {
|
||||
resolver = resolve
|
||||
})
|
||||
}
|
||||
|
||||
const onConfirm = () => {
|
||||
resolver?.(true)
|
||||
reset()
|
||||
}
|
||||
|
||||
const onCancel = () => {
|
||||
resolver?.(false)
|
||||
reset()
|
||||
}
|
||||
|
||||
return {
|
||||
visible,
|
||||
title,
|
||||
message,
|
||||
confirm,
|
||||
onConfirm,
|
||||
onCancel,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user