2021/9/18 全局自定义loading

loading.vue

<template>
  <div v-if="show">
    <div class="mask_loading">
      <img src="../assets/img/loading.gif" alt="" class="loading" />
    </div>
  </div>
</template>

<script>
export default {
  name: "loading",
  data() {
    return {};
  },
  props: {
    show: Boolean,
  },
};
</script>
<style scoped>
.loading {
  width: 400px;
  height: 300px;
  position: absolute;
  left: 45%;
  top: 50%;
  margin-left: -50px;
  margin-top: -40px;
  z-index: 13;
}
.mask_loading {
  width: 100%;
  height: 100%;
  background: rgba(7, 6, 6, 0.5);
  position: fixed;
  top: 0;
  z-index: 99999;
}
</style>

loading.js

import Vue from 'vue'
import loadingComponent from '../views/loading.vue'

const LoadingConstructor = Vue.extend(loadingComponent)

const instance = new LoadingConstructor({
    el: document.createElement('div')
})

instance.show = false // 默认隐藏
const loading = {
    show() { // 显示方法
        instance.show = true
        document.body.appendChild(instance.$el)
    },
    hide() { // 隐藏方法
        instance.show = false
    }
}

export default {
    install() {
        if (!Vue.$loading) {
            Vue.$loading = loading
        }
        Vue.mixin({
            created() {
                this.$loading = Vue.$loading
            }
        })
    }
}

main.js

// 引入全局loading
import loading from './js/loading'
// 全局使用loading
Vue.use(loading)

使用:

this.$loading.show(); // 显示loading
this.$loading.hide(); // 隐藏loading

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值