Vue-Router實(shí)現(xiàn)頁(yè)面正在加載特效方法示例
前言
vue-router是Vue.js官方的路由插件,它和vue.js是深度集成的,適合用于構(gòu)建單頁(yè)面應(yīng)用。vue的單頁(yè)面應(yīng)用是基于路由和組件的,路由用于設(shè)定訪問(wèn)路徑,并將路徑和組件映射起來(lái)。傳統(tǒng)的頁(yè)面應(yīng)用,是用一些超鏈接來(lái)實(shí)現(xiàn)頁(yè)面切換和跳轉(zhuǎn)的。在vue-router單頁(yè)面應(yīng)用中,則是路徑之間的切換,也就是組件的切換。
如果你在使用 Vue.js 和 Vue-Router 開(kāi)發(fā)單頁(yè)面應(yīng)用。因?yàn)槊總€(gè)頁(yè)面都是一個(gè) Vue 組件,你需要從服務(wù)器端請(qǐng)求數(shù)據(jù),然后再讓 Vue 引擎來(lái)渲染到頁(yè)面上。
例如,這里有個(gè)用戶個(gè)人資料的頁(yè)面。
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>
在動(dòng)畫(huà)過(guò)渡期間向服務(wù)器請(qǐng)求數(shù)據(jù),如下:
<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>
這樣,我們可以通過(guò)訪問(wèn)變量 $loadingRouteData。就可以實(shí)現(xiàn)隱藏所有的頁(yè)面元素,顯示某個(gè)正在加載的元素,比如某個(gè) 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 的正在加載時(shí)候的效果了。你可以把以上代碼寫(xiě)進(jìn)到一個(gè)單獨(dú)的組件內(nèi),在你用的地方引用它就行。
最后
這僅是個(gè)關(guān)于 Vue-Router 加載的組件的簡(jiǎn)單教程,實(shí)際上可以在許多地方來(lái)進(jìn)行改進(jìn),
如果你是一位對(duì) Vue.js 感興趣的前端工程師,可去這個(gè)網(wǎng)上瀏覽下,了解下國(guó)外對(duì) Vue 開(kāi)發(fā)者的要求。
好了,以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流。
相關(guān)文章
vue element table 表格請(qǐng)求后臺(tái)排序的方法
今天小編就為大家分享一篇vue element table 表格請(qǐng)求后臺(tái)排序的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
詳解vue 計(jì)算屬性與方法跟偵聽(tīng)器區(qū)別(面試考點(diǎn))
這篇文章主要介紹了詳解vue 計(jì)算屬性與方法跟偵聽(tīng)器區(qū)別(面試考點(diǎn)),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
創(chuàng)建和運(yùn)行Vue.js項(xiàng)目方法demo
這篇文章主要為大家介紹了創(chuàng)建和運(yùn)行Vue.js項(xiàng)目方法demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
vue監(jiān)聽(tīng)鍵盤(pán)事件的相關(guān)總結(jié)
這篇文章主要介紹了vue監(jiān)聽(tīng)鍵盤(pán)事件的相關(guān)總結(jié),幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下2021-01-01
Vue打包優(yōu)化之生產(chǎn)環(huán)境刪除console日志配置
這篇文章主要為大家介紹了Vue打包優(yōu)化之生產(chǎn)環(huán)境刪除console日志配置詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06

