ant design vue全局loading

1.封装loading (SpinLoader.js)

import { createApp, h } from 'vue'
import { Spin } from 'ant-design-vue'

let instance = null

// 定义全屏遮罩样式
const style = {
    position: 'fixed',
    left: 0,
    top: 0,
    width: '100%',
    height: '100%',
    display: 'flex',
    justifyContent: 'center',
    alignItems: 'center',
    background: 'rgba(0, 0, 0, .5)',
    zIndex: 99999
}

function getInstance () {
    if (!instance) {
        const vn = createApp({
            data () {
                return {
                    show: false,
                    message: 'Loading...',
                    timeoutId: 'loader'
                }
            },
            unmounted () {
                if (instance && instance.$el) {
                    instance.$el.remove() // 当组件卸载时,从DOM中移除元素以避免内存泄漏
                }
            },
            methods: {
                loading (val, timeout) {
                    this.show = true
                    this.message = val || 'Loading...'
                    if (timeout) {
                        this.timeoutId = setTimeout(() => {
                            this.close()
                        }, timeout) // 超时时间单位为毫秒
                    }
                },
                close () {
                    clearTimeout(this.timeoutId) // 清除定时器
                    this.show = false
                }
            },
            render () {
                return this.show
                    ? h('div', { style }, [h(Spin, { tip: this.message })])
                    : null
            }
        })
        const ele = document.createElement('div')
        instance = vn.mount(ele)
        document.body.appendChild(ele)
    }
    return instance
}

export default {
    ...Spin,
    show (val, timeout) {
        getInstance().loading(val, timeout)
    },
    hide () {
        getInstance().close()
    }
}

2.页面使用代码

import Spin from '@/components/antAssembly/SpinLoader'


以下是两种,可设置默认时间
// Spin.show('Loading...', 2000)
Spin.show('Loading...')
Spin.hide()
  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值