vue制作全局loading

最近做项目的时候,发现自定义loading组件,发现网上的loading组件主要有两种情况:

1.是组件开发,这样开发需要在引用的地方都要引入一遍,感觉比较麻烦。

 <template>
     <div class="loading">
       <img width="24" height="24" src="./loading.gif">
       <p class="desc">{{title}}</p>
     </div>
</template>
<script type="text/ecmascript-6">
   export default {
       props: {
           title: {
             type: String,
             default: '正在载入...'
           }
     }
 }
</script>
<style scoped>
 .loading{
   width: 100%;
   text-align: center;
  }
 .desc{
   line-height: 20px;
   font-size: 12px;
   color: white;
 }
</style>

这样每次使用的时候

<template>
 <div class="music-list">
   <loading></loading>
 </div>
</template>
<script type="text/ecmascript-6">
 import Loading from 'base/loading/loading'
 export default {
   components: {
     Loading
   }
 }
</script>

2.是使用install注册组件(Vue.use()来使用)

import './index.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 

将该组件导出,这里就存在一个关键点:install, 只有使用install了,我们在main.js中,才能够直接use该组件,而install函数默认自带一个参数Vue,也就是我们正在使用的Vue对象,然后定义loading这一组件,使用$mount()给组件手动挂载参数,然后将组件插入页面中。

index.css

#vue-loading{
  width: 100%;
  height: 100%;
  position: absolute;
  left: 0;
  top: 0;
}
#vue-loading .loader{}
  position: absolute;
  width: 2.5em;
  height: 2.5em;
  top: calc(50% - 1.25em);
  left: calc(50% - 1.25em);
  transform: rotate(165deg);
}
#vue-loading .loader:before, #vue-loading .loader:after{
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  display: block;
  width: 0.5em;
  height: 0.5em;
  border-radius: 0.25em;
  transform: translate(-50%, -50%);
}
#vue-loading .loader:before{
  animation: before 2s infinite;
}
#vue-loading .loader:after{
  animation: after 2s infinite;
}
.loading-context{
  position: absolute;
  width: 100%;
  top: calc(50% + 2.25em);
  text-align: center;
  height: 30px;
  line-height: 30px;
}
 
 
@keyframes before {
  0% {
    width: 0.5em;
    box-shadow: 1em -0.5em rgba(225, 20, 98, 0.75), -1em 0.5em rgba(111, 202, 220, 0.75);
  }
  35% {
    width: 2.5em;
    box-shadow: 0 -0.5em rgba(225, 20, 98, 0.75), 0 0.5em rgba(111, 202, 220, 0.75);
  }
  70% {
    width: 0.5em;
    box-shadow: -1em -0.5em rgba(225, 20, 98, 0.75), 1em 0.5em rgba(111, 202, 220, 0.75);
  }
  100% {
    box-shadow: 1em -0.5em rgba(225, 20, 98, 0.75), -1em 0.5em rgba(111, 202, 220, 0.75);
  }
}
 
@keyframes after {
  0% {
    height: 0.5em;
    box-shadow: 0.5em 1em rgba(61, 184, 143, 0.75), -0.5em -1em rgba(233, 169, 32, 0.75);
  }
  35% {
    height: 2.5em;
    box-shadow: 0.5em 0 rgba(61, 184, 143, 0.75), -0.5em 0 rgba(233, 169, 32, 0.75);
  }
  70% {
    height: 0.5em;
    box-shadow: 0.5em -1em rgba(61, 184, 143, 0.75), -0.5em 1em rgba(233, 169, 32, 0.75);
  }
  100% {
    box-shadow: 0.5em 1em rgba(61, 184, 143, 0.75), -0.5em -1em rgba(233, 169, 32, 0.75);
  }
}

使用:

// 在main.js中引入
import loading from "@/base/loading"

// 然后通过 USE方法全局注册
Vue.use(Loading)

// 全局调用
this.$loading.show(options) 
this.$loading.hide(options)

但是这种方法,只能在vue实例存在的页面中使用,比如重新封装ajax,在ajax里添加loading,这个时候往往ajax都是封装在单独的js文件中,此时,vue实例不存在,就无法使用。故在上面的基础上改进如下版本:

import './index.css'
 let installed = false
// 避免重复install,设立flag
const loading =  {
 show(context = '') {
  if (installed) return
 // 如果页面有loading则不继续执行
 if (document.querySelector('#vue-loading')) return
  // 1、创建构造器,定义loading模板
  let divWrapper = document.createElement('div')
  divWrapper.setAttribute("id","vue-loading")
  let div = document.createElement('div')
  div.setAttribute('class','loader')
  let span = document.createElement('span')
  span.className = 'loading-context'
  span.innerHTML = context
  divWrapper.appendChild(div)
  divWrapper.appendChild(span)
  // 3、把创建的实例添加到body中
  document.body.appendChild(divWrapper)
  // 阻止遮罩滑动
  document.querySelector('#vue-loading').addEventListener('touchmove', function (e) {
    e.stopPropagation()
    e.preventDefault()
  })
  installed = true
 },
 hide() {
  let tpl = document.querySelector('#vue-loading')
  if(!tpl) return
  document.body.removeChild(tpl)
 }
}
export default loading

index.css

#vue-loading{
  width: 100%;
  height: 100%;
  position: absolute;
  left: 0;
  top: 0;
}
#vue-loading .loader{}
  position: absolute;
  width: 2.5em;
  height: 2.5em;
  top: calc(50% - 1.25em);
  left: calc(50% - 1.25em);
  transform: rotate(165deg);
}
#vue-loading .loader:before, #vue-loading .loader:after{
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  display: block;
  width: 0.5em;
  height: 0.5em;
  border-radius: 0.25em;
  transform: translate(-50%, -50%);
}
#vue-loading .loader:before{
  animation: before 2s infinite;
}
#vue-loading .loader:after{
  animation: after 2s infinite;
}
.loading-context{
  position: absolute;
  width: 100%;
  top: calc(50% + 2.25em);
  text-align: center;
  height: 30px;
  line-height: 30px;
}
 
 
@keyframes before {
  0% {
    width: 0.5em;
    box-shadow: 1em -0.5em rgba(225, 20, 98, 0.75), -1em 0.5em rgba(111, 202, 220, 0.75);
  }
  35% {
    width: 2.5em;
    box-shadow: 0 -0.5em rgba(225, 20, 98, 0.75), 0 0.5em rgba(111, 202, 220, 0.75);
  }
  70% {
    width: 0.5em;
    box-shadow: -1em -0.5em rgba(225, 20, 98, 0.75), 1em 0.5em rgba(111, 202, 220, 0.75);
  }
  100% {
    box-shadow: 1em -0.5em rgba(225, 20, 98, 0.75), -1em 0.5em rgba(111, 202, 220, 0.75);
  }
}
 
@keyframes after {
  0% {
    height: 0.5em;
    box-shadow: 0.5em 1em rgba(61, 184, 143, 0.75), -0.5em -1em rgba(233, 169, 32, 0.75);
  }
  35% {
    height: 2.5em;
    box-shadow: 0.5em 0 rgba(61, 184, 143, 0.75), -0.5em 0 rgba(233, 169, 32, 0.75);
  }
  70% {
    height: 0.5em;
    box-shadow: 0.5em -1em rgba(61, 184, 143, 0.75), -0.5em 1em rgba(233, 169, 32, 0.75);
  }
  100% {
    box-shadow: 0.5em 1em rgba(61, 184, 143, 0.75), -0.5em -1em rgba(233, 169, 32, 0.75);
  }
}

使用:

// 在使用的页面中引入
import loading from "@/base/loading"

// 使用
loading.show()
loading.hide()

// 也可以在main.js将组将挂载到vue实例中

Vue.prototype.loading = loading

// 使用
this.$loading.show() 
this.$loading.hide() 

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一:什么是闭包?闭包的用处? (1)闭包就是能够读取其他函数内部变量的函数。在本质上,闭包就 是将函数内部和函数外...
    xuguibin阅读 9,751评论 1 52
  • ## 框架和库的区别?> 框架(framework):一套完整的软件设计架构和**解决方案**。> > 库(lib...
    Rui_bdad阅读 2,979评论 1 4
  • Vue 实例 属性和方法 每个 Vue 实例都会代理其 data 对象里所有的属性:var data = { a:...
    云之外阅读 2,252评论 0 6
  • 独卧榻前难安眠,大雨滂沱帷幔湿。春风不解兰花意,玉萧万里递情思。
    婼離阅读 285评论 0 4
  • 既然框架能跑起来,接下来当然是分析代码。在My First Spring-Boot Project(1)提到需要添...
    Benson_Lim阅读 167评论 0 0