国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

Vue-Router實現頁面正在加載特效方法示例

2019-11-19 17:37:58
字體:
來源:轉載
供稿:網友

前言

vue-router是Vue.js官方的路由插件,它和vue.js是深度集成的,適合用于構建單頁面應用。vue的單頁面應用是基于路由和組件的,路由用于設定訪問路徑,并將路徑和組件映射起來。傳統的頁面應用,是用一些超鏈接來實現頁面切換和跳轉的。在vue-router單頁面應用中,則是路徑之間的切換,也就是組件的切換。

如果你在使用 Vue.js 和 Vue-Router 開發單頁面應用。因為每個頁面都是一個 Vue 組件,你需要從服務器端請求數據,然后再讓 Vue 引擎來渲染到頁面上。

例如,這里有個用戶個人資料的頁面。

user.vue 文件如下:

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

在動畫過渡期間向服務器請求數據,如下:

<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>

這樣,我們可以通過訪問變量 $loadingRouteData。就可以實現隱藏所有的頁面元素,顯示某個正在加載的元素,比如某個 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>

比如,正在加載的樣式代碼如下:

.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;}

這樣,你就有了 Vue-Router 的正在加載時候的效果了。你可以把以上代碼寫進到一個單獨的組件內,在你用的地方引用它就行。

最后

這僅是個關于 Vue-Router 加載的組件的簡單教程,實際上可以在許多地方來進行改進,

VueJobs.com

如果你是一位對 Vue.js 感興趣的前端工程師,可去這個網上瀏覽下,了解下國外對 Vue 開發者的要求。

好了,以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 余庆县| 武胜县| 柏乡县| 平度市| 寻乌县| 克山县| 勃利县| 阿克苏市| 济宁市| 新乐市| 濮阳市| 大邑县| 吉木乃县| 宁城县| 进贤县| 鄢陵县| 天柱县| 泾阳县| 洪雅县| 阿克苏市| 呼伦贝尔市| 宁都县| 新乐市| 恩施市| 澜沧| 威远县| 于田县| 深泽县| 大埔县| 红桥区| 庐江县| 双柏县| 西乌| 庆安县| 库车县| 阆中市| 印江| 桂东县| 乌兰察布市| 辽阳县| 田东县|