useModal.js
| import { nextTick, ref } from 'vue' |
| import { isFunction } from '@/utils/judge' |
| |
| export function useModal() { |
| |
| |
| const visible = ref(false) |
| const loading = ref(false) |
| |
| const hideModal = () => { |
| visible.value = false |
| } |
| |
| const showModal = () => { |
| visible.value = true |
| } |
| |
| const toggleLoading = () => { |
| loading.value = !loading.value |
| } |
| const handleOk = async (callbackFn) => { |
| if (!callbackFn || !isFunction(callbackFn)) return |
| toggleLoading() |
| console.log('output-> 1', 1) |
| callbackFn(() => { |
| console.log('output-> 2', 2) |
| toggleLoading(); |
| hideModal(); |
| }) |
| } |
| |
| return { |
| visible, |
| showModal, |
| hideModal, |
| handleOk, |
| loading, |
| toggleLoading |
| } |
| } |
| |
使用案例
| <template> |
| <div> |
| <a-button type="link" @click="showModal">详情</a-button> |
| <a-modal v-model:visible="visible" |
| title="登录表单" |
| > |
| <a-form |
| ref="formRef" |
| > |
| <a-form-item name="name"> |
| <a-input |
| v-model:value="state.userName" |
| autocomplete="off" |
| placeholder="请输入账号" |
| > |
| <template v-slot:prefix> |
| <UserOutlined style="color: rgba(0, 0, 0, 0.25)" /> |
| </template> |
| </a-input> |
| </a-form-item> |
| </a-form> |
| <template #footer> |
| <a-button key="back" @click="hideModal">取消</a-button> |
| <a-button key="submit" |
| type="primary" :loading="loading" |
| @click="handleOk(submitFn)">提交</a-button> |
| </template> |
| </a-modal> |
| </div> |
| </template> |
| <script setup> |
| import { useModal } from '@/hooks/useModal' |
| import { message } from 'ant-design-vue' |
| import { UserOutlined } from '@ant-design/icons-vue' |
| import { reactive } from 'vue' |
| |
| let {visible, loading, handleOk, hideModal, showModal} = useModal(); |
| |
| const state = reactive({ |
| userName: '' |
| }) |
| const submitFn = async (callback) => { |
| setTimeout(() => { |
| |
| console.log('output-> state.userName::: ', state.userName) |
| message.warn('提交成功') |
| callback() |
| }, 2000) |
| return true; |
| } |
| |
| |
| </script> |
| |
| |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具