Vue Router implementation page loading effect method sample

  • 2021-07-18 06:59:59
  • OfStack

Preface

vue-router is the official routing plug-in of Vue. js, which is deeply integrated with vue. js and is suitable for building single-page applications. The single page application of vue is based on routes and components. Routing is used to set access paths and map paths and components. Traditional page application uses 1 hyperlink to realize page switching and jump. In vue-router single page application, it is the switching between paths, that is, the switching of components.

If you are using Vue. js and Vue-Router to develop a single page application. Since each page is an Vue component, you need to request data from the server and then have the Vue engine render it onto the page.

For example, there is a page of user profile.

The file user. vue is as follows:


<template>
 <div>
  <h2 v-text="user.name"></h2>
  <p v-text="user.description"></p>
 </div>
</template>
<script>
 export default{
  data(){
   return{
    user: {}
   }
  }
 }
</script>

Request data from the server during animation transition, as follows:


<script>
export default{
 data(){
  return{
   user: {}
  }
 },
 route: {
  data: function (transition) {
   this.getUserDetails(transition);
  }
 },
 methods: {
  getUserDetails(transition)
  {
   this.$http.get('/users/' + this.$route.params.userName)
    .then(function (response) {
     this.user = response.data;
     transition.next();
    });
  }
 }
}
</script>

In this way, we can access the variable $loadingRouteData. You can hide all the page elements and show an element that is being loaded, such as an logo.


<div v-if="$loadingRouteData">
 <div class="white-widget grey-bg author-area">
 <div class="auth-info row">
 <div class="timeline-wrapper">
 <div class="timeline-item">
  <div class="animated-background">
   <div class="background-masker header-top"></div>
   <div class="background-masker header-left"></div>
   <div class="background-masker header-right"></div>
   <div class="background-masker header-bottom"></div>
   <div class="background-masker subheader-left"></div>
   <div class="background-masker subheader-right"></div>
   <div class="background-masker subheader-bottom"></div>
  </div>
 </div>
 </div>
 </div>
 </div>
</div>
<div v-if="!$loadingRouteData">
 <div>
  <h2 v-text="user.name"></h2>
  <p v-text="user.description"></p>
 </div>
</div>

For example, the style code being loaded is as follows:


.timeline-item {
 background: #fff;
 border-bottom: 1px solid #f2f2f2;
 padding: 25px;
 margin: 0 auto;
}

@keyframes placeHolderShimmer{
 0%{
 background-position: -468px 0
 }
 100%{
 background-position: 468px 0
 }
}

.animated-background {
 animation-duration: 1s;
 animation-fill-mode: forwards;
 animation-iteration-count: infinite;
 animation-name: placeHolderShimmer;
 animation-timing-function: linear;
 background: #f6f7f8;
 background: linear-gradient(to right, #eeeeee 8%, #dddddd 18%, #eeeeee 33%);
 background-size: 800px 104px;
 height: 40px;
 position: relative;
}

.background-masker {
 background: #fff;
 position: absolute;
}

/* Every thing below this is just positioning */

.background-masker.header-top,
.background-masker.header-bottom,
.background-masker.subheader-bottom {
 top: 0;
 left: 40px;
 right: 0;
 height: 10px;
}

.background-masker.header-left,
.background-masker.subheader-left,
.background-masker.header-right,
.background-masker.subheader-right {
 top: 10px;
 left: 40px;
 height: 8px;
 width: 10px;
}

.background-masker.header-bottom {
 top: 18px;
 height: 6px;
}

.background-masker.subheader-left,
.background-masker.subheader-right {
 top: 24px;
 height: 6px;
}


.background-masker.header-right,
.background-masker.subheader-right {
 width: auto;
 left: 300px;
 right: 0;
}

.background-masker.subheader-right {
 left: 230px;
}

.background-masker.subheader-bottom {
 top: 30px;
 height: 10px;
}

.background-masker.content-top,
.background-masker.content-second-line,
.background-masker.content-third-line,
.background-masker.content-second-end,
.background-masker.content-third-end,
.background-masker.content-first-end {
 top: 40px;
 left: 0;
 right: 0;
 height: 6px;
}

.background-masker.content-top {
 height:20px;
}

.background-masker.content-first-end,
.background-masker.content-second-end,
.background-masker.content-third-end{
 width: auto;
 left: 380px;
 right: 0;
 top: 60px;
 height: 8px;
}

.background-masker.content-second-line {
 top: 68px;
}

.background-masker.content-second-end {
 left: 420px;
 top: 74px;
}

.background-masker.content-third-line {
 top: 82px;
}

.background-masker.content-third-end {
 left: 300px;
 top: 88px;
}

In this way, you have the effect of Vue-Router when it is loading. You can write the above code into a separate component and refer to it where you use it.

Finally

This is just a simple tutorial on the components loaded by Vue-Router, which can actually be improved in many places,

VueJobs.com

If you are a front-end engineer interested in Vue. js, you can browse this website to understand the requirements of Vue developers abroad.

Well, the above is the whole content of this article. I hope the content of this article can bring 1 certain help to your study or work. If you have any questions, you can leave a message for communication.


Related articles: