Vue-Router實作頁面正在載入特效方法範例
這篇文章主要給大家介紹了利用Vue-Router實現頁面正在加載特效方法示例,文中給出了詳細的示例代碼,相信對大家具有一定的參考價值,有需要的朋友們下面來一起看看吧。
前言
vue-router是Vue.js官方的路由插件,它和vue.js是深度整合的,適合用於建立單頁應用。 vue的單一頁面應用程式是基於路由和元件的,路由用於設定存取路徑,並將路徑和元件對應起來。傳統的頁面應用,是用一些超連結來實現頁面切換和跳轉的。在vue-router單一頁面應用程式中,則是路徑之間的切換,也就是元件的切換。
如果你在使用 Vue.js 和 Vue-Router 開發單一頁面應用程式。因為每個頁面都是一個 Vue 元件,你需要從伺服器端請求數據,然後再讓 Vue 引擎來渲染到頁面上。
例如,這裡有個使用者個人資料的頁面。
user.vue 檔案如下:
<template> <p> <h2 v-text="user.name"></h2> <p v-text="user.description"></p> </p> </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>
就可以實現隱藏所有的頁面元素,顯示某個正在載入的元素,例如某個 logo 等。
<p v-if="$loadingRouteData"> <p class="white-widget grey-bg author-area"> <p class="auth-info row"> <p class="timeline-wrapper"> <p class="timeline-item"> <p class="animated-background"> <p class="background-masker header-top"></p> <p class="background-masker header-left"></p> <p class="background-masker header-right"></p> <p class="background-masker header-bottom"></p> <p class="background-masker subheader-left"></p> <p class="background-masker subheader-right"></p> <p class="background-masker subheader-bottom"></p> </p> </p> </p> </p> </p> </p> <p v-if="!$loadingRouteData"> <p> <h2 v-text="user.name"></h2> <p v-text="user.description"></p> </p> </p>
例如,正在加載的樣式代碼如下:
.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 加載的組件的簡單教程,實際上可以在許多地方來進行改進,
如果你是一位感.有興趣的前端工程師,可到這個網路瀏覽下,了解下國外對Vue 開發者的要求。
🎜更多Vue-Router實作頁面正在載入特效方法範例相關文章請關注PHP中文網! 🎜
熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

本文討論了在瀏覽器中優化JavaScript性能的策略,重點是減少執行時間並最大程度地減少對頁面負載速度的影響。

本文討論了使用瀏覽器開發人員工具的有效JavaScript調試,專注於設置斷點,使用控制台和分析性能。

Python和JavaScript開發者的薪資沒有絕對的高低,具體取決於技能和行業需求。 1.Python在數據科學和機器學習領域可能薪資更高。 2.JavaScript在前端和全棧開發中需求大,薪資也可觀。 3.影響因素包括經驗、地理位置、公司規模和特定技能。

本文說明瞭如何使用源地圖通過將其映射回原始代碼來調試JAVASCRIPT。它討論了啟用源地圖,設置斷點以及使用Chrome DevTools和WebPack之類的工具。

如何在JavaScript中將具有相同ID的數組元素合併到一個對像中?在處理數據時,我們常常會遇到需要將具有相同ID�...

深入探討console.log輸出差異的根源本文將分析一段代碼中console.log函數輸出結果的差異,並解釋其背後的原因。 �...
