vue添加全局loading

上图不上种,菊花万人捅:

loading.js:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

import './loading.css'

 

let Loading = {}

// 避免重复install,设立flag

Loading.installed = false

Loading.install = function (Vue) {

  if (Loading.installed) return

  Vue.prototype.$loading = {}

  Vue.prototype.$loading.show = () => {

    // 如果页面有loading则不继续执行

    if (document.querySelector('#vue-loading')) return

    // 1、创建构造器,定义loading模板

    let LoadingTip = Vue.extend({

      template: `<div id="vue-loading">

                    <div class="loader"></div>

                  </div>`

    })

    // 2、创建实例,挂载到文档以后的地方

    let tpl = new LoadingTip().$mount().$el

    // 3、把创建的实例添加到body中

    document.body.appendChild(tpl)

    // 阻止遮罩滑动

    document.querySelector('#vue-loading').addEventListener('touchmove'function (e) {

      e.stopPropagation()

      e.preventDefault()

    })

    Loading.installed = true

  }

  Vue.prototype.$loading.hide = () => {

    let tpl = document.querySelector('#vue-loading')

    document.body.removeChild(tpl)

  }

}

 

export default Loading

  loading.css:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

#vue-loading {

  width100%;

  height100%;

  positionabsolute;

  left0;

  top0

}

 

.loader {

  positionrelative;

  width2.5em;

  height2.5em;

  transform: rotate(165deg);

}

 

.loader:before, .loader:after {

  content'';

  positionabsolute;

  top50%;

  left50%;

  displayblock;

  width0.5em;

  height0.5em;

  border-radius: 0.25em;

  transform: translate(-50%-50%);

}

 

.loader:before {

  animation: before 2s infinite;

}

 

.loader:after {

  animation: after 2s infinite;

}

 

@keyframes before {

  0% {

    width0.5em;

    box-shadow: 1em -0.5em rgba(22520980.75), -1em 0.5em rgba(1112022200.75);

  }

  35% {

    width2.5em;

    box-shadow: 0 -0.5em rgba(22520980.75), 0 0.5em rgba(1112022200.75);

  }

  70% {

    width0.5em;

    box-shadow: -1em -0.5em rgba(22520980.75), 1em 0.5em rgba(1112022200.75);

  }

  100% {

    box-shadow: 1em -0.5em rgba(22520980.75), -1em 0.5em rgba(1112022200.75);

  }

}

 

@keyframes after {

  0% {

    height0.5em;

    box-shadow: 0.5em 1em rgba(611841430.75), -0.5em -1em rgba(233169320.75);

  }

  35% {

    height2.5em;

    box-shadow: 0.5em 0 rgba(611841430.75), -0.5em 0 rgba(233169320.75);

  }

  70% {

    height0.5em;

    box-shadow: 0.5em -1em rgba(611841430.75), -0.5em 1em rgba(233169320.75);

  }

  100% {

    box-shadow: 0.5em 1em rgba(611841430.75), -0.5em -1em rgba(233169320.75);

  }

}

 

/**

 * Attempt to center the whole thing!

 */

html,

body {

  height100%;

}

 

.loader {

  positionabsolute;

  top: calc(50% 1.25em);

  left: calc(50% 1.25em);

}

  在main.js里面 :

   import Vue from 'vue';
   import Loading from '@/components/loading'
   Vue.use(Loading)

  这样就可以直接在组件里面手动调用啦!

  调用方法: this.$loading.show(),  this.$loading.hide()

  因为这个项目暂时规模很小,就只有注册功能,我直接把调用写在axios请求拦截器里面,每次请求和请求结束都会调用loading,这样就不用在页面里面手动调用啦。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值