vue项目中loading全局配置,(超方便)两种方法

第一种方法简单操作

1、首先写一个公共组件,然后再建立一个js文件将其引入并使用
js文件

import topbar from '../components/navigation'

export default (Vue) => {
  Vue.component('topbar', topbar)
}

2、最后在入口main.js引入这个js文件并使用

import globalVue from './globalVue.js'
Vue.use(globalVue)

3、然后vue组件中直接使用即可
在这里插入图片描述

第二种方法一次封装,使用超方便

1、建立一个loading的文件夹,存放至少一个js一个vue文件,如果需要图片也可放在里面
在这里插入图片描述
2、vue文件(里面用的vant)

<template>
  <div v-if="show">
    <van-overlay class="box" :show="true">
      <div class="wrapper" @click.stop>
        <van-loading class="block" size="50" />
      </div>
    </van-overlay>
  </div>
</template>
<script>
export default {
  name: "Loading",
  props: {
    show: Boolean,
  },
};
</script>

<style lang="scss" scoped>
.box {
  width: 100vw;
  height: 100vh;
  position: fixed;
  z-index: 9999;
  .wrapper {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    .block {
      color: #6a72fe;
    }
  }
}
</style>

3、js文件

import Vue from "vue";
import componentLoading from "./loading.vue";

const comLoading = Vue.extend(componentLoading);

const instance = new comLoading({
  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;
      },
    });
  },
};

4、在main.js入口文件直接引入并使用

import loadong from "./loading/loading";
Vue.use(loadong);

5、使用方法

全局使用:
	this.$loading.show();
    this.$loading.hide();
  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值