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>
这样,我们可以通过访问变量 $loadingRouteData。就可以实现隐藏所有的页面元素,显示某个正在加载的元素,比如某个 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 加载的组件的简单教程,实际上可以在许多地方来进行改进,
VueJobs.com
如果你是一位对 Vue.js 感兴趣的前端工程师,可去这个网上浏览下,了解下国外对 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函数输出结果的差异,并解释其背后的原因。�...
