目的

在前后端分离项目中,由于页面请求和数据请求并非同步,所以导致页面和数据不能同时渲染,因此在实际过程中往往采用SSR即服务端渲染或者请求数据时采用遮罩(加载中)的方式提升用户体验。下面我将使用loading遮罩的方式实现更加友好的数据加载。

参考

https://codepen.io/bartezic/pen/ByqeNq

效果

https://res.cloudinary.com/loneykids/image/upload/v1611242415/2021/18点26分_cbrmjz.gif

代码

  1. 在 App.vue 中添加一个<div>
<div>
    <div id="appLoading">
      <div class='lmask'></div>
    </div>
    <div id="app">
      <router-view v-if="isRouterAlive"/>
    </div>
 </div>
  1. 以下是 CSS 样式,采用 less 预编译
.lmask {
  position: absolute;
  height: 100%;
  width: 100%;
  background-color: #000;
  bottom: 0;
  left: 0;
  right: 0;
  top: 0;
  z-index: 9999;;
  opacity: 0.4;
  &.fixed {
    position: fixed;
  }
  &:before {
    content: '';
    background-color: rgba(0,0,0,0);
    border: 5px solid rgba(0,183,229,0.9);
    opacity: .9;
    border-right: 5px solid rgba(0,0,0,0);
    border-left: 5px solid rgba(0,0,0,0);
    border-radius: 50px;
    box-shadow: 0 0 35px #2187e7;
    width: 50px;
    height: 50px;
    -moz-animation: spinPulse 1s infinite ease-in-out;
    -webkit-animation: spinPulse 1s infinite linear;

    margin: -25px 0 0 -25px;
    position: absolute;
    top: 50%;
    left: 50%;
  }
  &:after {
    content: '';
    background-color: rgba(0,0,0,0);
    border: 5px solid rgba(0,183,229,0.9);
    opacity: .9;
    border-left: 5px solid rgba(0,0,0,0);
    border-right: 5px solid rgba(0,0,0,0);
    border-radius: 50px;
    box-shadow: 0 0 15px #2187e7;
    width: 30px;
    height: 30px;
    -moz-animation: spinoffPulse 1s infinite linear;
    -webkit-animation: spinoffPulse 1s infinite linear;

    margin: -15px 0 0 -15px;
    position: absolute;
    top: 50%;
    left: 50%;
  }
}

@-moz-keyframes spinPulse {
  0% {
    -moz-transform:rotate(160deg);
    opacity: 0;
    box-shadow: 0 0 1px #2187e7;
  }
  50% {
    -moz-transform: rotate(145deg);
    opacity: 1;
  }
  100% {
    -moz-transform: rotate(-320deg);
    opacity: 0;
  }
}
@-moz-keyframes spinoffPulse {
  0% {
    -moz-transform: rotate(0deg);
  }
  100% {
    -moz-transform: rotate(360deg);
  }
}
@-webkit-keyframes spinPulse {
  0% {
    -webkit-transform: rotate(160deg);
    opacity: 0;
    box-shadow: 0 0 1px #2187e7;
  }
  50% {
    -webkit-transform: rotate(145deg);
    opacity: 1;
  }
  100% {
    -webkit-transform: rotate(-320deg);
    opacity: 0;
  }
}
@-webkit-keyframes spinoffPulse {
  0% {
    -webkit-transform: rotate(0deg);
  }
  100% {
    -webkit-transform: rotate(360deg);
  }
}
  1. 此时遮罩层显示,我们需要当App加载完成后取消遮罩层,即display: none
	document.getElementById('app').style.display = 'block';
	document.getElementById('appLoading').style.display = 'none';
  1. 如何在我们需要的地方调用呢我采用的方法是写一个loading和loaded函数,在分别在请求数据前和请求数据完成后调用,因为该功能可能会重复的使用,所以将其封装成为一个可以复用的模块在App.vue中写两个方法:
	provide() {
	    return {
	      loading: this.loading,
	      loaded: this.loaded
	    }
	  },
	  methods: {
		loading() {
		      document.getElementById('app').style.display = 'block';
		      document.getElementById('appLoading').style.display = 'block';
		},
	    loaded() {
	      document.getElementById('app').style.display = 'block';
	      document.getElementById('appLoading').style.display = 'none';
	    }
	  }

上面将这两个函数给暴露了出来,在我们需要的地方inject就好

  1. 在需要使用的地方调用
export default {
  inject: ['loading', 'loaded'],
  name: "index",
  created() {
    this.getData()
  },
  methods: {
    getData() {
      this.loading()
      getUserInfo().then(res => {
        this.userInfo = res.data
      }).finally(() => {
        this.loaded()
      })
    }
  }
}
  1. 完成

总结