首页 > 其他分享 >vue路由加载页面

vue路由加载页面

时间:2022-10-08 17:27:06浏览次数:71  
标签:loading const vue div 路由 加载

当vue路由切换时,有时候会出现短暂白屏,需要添加一个加载状态

参考:buildadmin 地址:https://demo.buildadmin.com/#/

利用vue的路由导航守卫:beforeEachafterEach来判断显示加载状态的时间,当路由进入beforeEach开始显示加载页面,当路由进入afterEach结束加载状态

路由守卫

import { loading } from '@/utils/loading'

router.beforeEach((to, from, next) => {
   // 页面加载
  if (!(window as any).existLoading) {
    loading.show();
    (window as any).existLoading = true
  }
})

router.afterEach(() => {
  if ((window as any).existLoading) {
      loading.hide()
  }
})

加载组件 loading.ts

import { nextTick } from 'vue'
import '@/styles/loading.scss'

export const loading = {
    show: () => {
        const bodys: Element = document.body
        const div = document.createElement('div')
        div.className = 'block-loading'
        div.innerHTML = `
            <div class="block-loading-box">
                <div class="block-loading-box-warp">
                    <div class="block-loading-box-item"></div>
                    <div class="block-loading-box-item"></div>
                    <div class="block-loading-box-item"></div>
                    <div class="block-loading-box-item"></div>
                    <div class="block-loading-box-item"></div>
                    <div class="block-loading-box-item"></div>
                    <div class="block-loading-box-item"></div>
                    <div class="block-loading-box-item"></div>
                    <div class="block-loading-box-item"></div>
                </div>
            </div>
        `
        bodys.insertBefore(div, bodys.childNodes[0])
    },
    hide: () => {
        nextTick(() => {
            setTimeout(() => {
                const el = document.querySelector('.block-loading')
                el && el.parentNode?.removeChild(el)
            }, 200)
        })
    },
}

标签:loading,const,vue,div,路由,加载
From: https://www.cnblogs.com/my-wl/p/16769565.html

相关文章

  • 自定义RecyclerView下拉刷新上拉加载更多
    自定义ListView下拉刷新上拉加载更多自定义RecyclerView下拉刷新上拉加载更多......
  • 【Vue3.x】pinia
    PiniaVue3中使用Pinia替代vuex更改如下:支持ts体积小,压缩后1KB去除mutations,只有state,getters,actions;去除mutations后,actions直接进行同步和异步操作修改数据去......
  • vue-2 模板语法
    router/index.js//1、引入基础依赖importVuefrom'vue'importRouterfrom'vue-router'//2、引入要路由的页面importSmartyfrom"../components/smarty"//3、......
  • vue的computed计算属性的执行机制
    vue中初始化computed,每一个计算属性的本质就是watcher,创建计算属性的watcher的时候,会传入一个懒惰属性,来控制computed缓存,默认是执行的,先处理为vm._computedWatchers对象......
  • vuecli build 代码拆解
    splitChunks:{//表示选择哪些chunks进行分割,可选值有:async,initial和allchunks:"async",//表示新分离出的chunk必须大于等于minSize,默认为30000,约3......
  • vuex中commit
    一、不使用模块的基础模式看vuex相关的文件夹,放在src下的store文件夹,里面有一个index.js文件,为vuex的入口,如果不使用模块,可以将所有相关代码写在index.js文件里面,下面是最......
  • 没有公网ip,如何在路由器中端口映射?
    路由器端口映射的前提是需要有公网IP,在没有公网IP情况下做端口映射也是无效的。其实路由器做端口映射,其主要作用就是让公网用户可以访问到内网中的服务器。而普遍情况下,路由......
  • drf之路由类与认证类
    一、自动生成路由对于我们的视图类只要继承了ViewSetMixin及其子类那么路由的写法就需要改变path('books/',views.BookView.as_view({'get':'list'}))这个时候有人写......
  • 路由组件
    路由组件自动生成路由action装饰器的使用路由Routers对于视图集ViewSet,我们除了可以自己手动指明请求方式与动作action之间的对应关系外,还可以使用Routers来帮......
  • vue.js3:axios图片上传([email protected] / [email protected])
    一,安装axios库1,相关地址官网:https://axios-http.com/代码地址:https://github.com/axios/axios2,安装liuhongdi@lhdpc:/data/vue/axios$npminstall--......