首页 > 其他分享 >vue-router4 配置懒加载 页面加载时展示loading

vue-router4 配置懒加载 页面加载时展示loading

时间:2023-04-26 16:22:54浏览次数:37  
标签:vue componentPath component router4 loading 组件 加载

 

懒加载写法

{
    path: "/",
    name: "index",
    component: () => import("../views/Home.vue"),
  }

  

创建Loading组件 并引入到顶层组件中 使用store控制loading组件是否展示

包装懒加载写法

const lazyLoad = (componentPath) => {
  // 注意:componentPath 不能直接赋值 import(componentPath),直接赋值无法找到组件
  return () => new Promise((resolve, reject) => {
    const timer = setTimeout(() => {
      store.commit('showLoading');
    }, 500);
    import(`@views/${componentPath}.vue`).then((component) => {
      clearTimeout(timer);
      store.commit('hideLoading');
      resolve(component)
    })
  })
}


export const routes: Array<RouteRecordRaw> = [
    
{
    path: "/",
    name: "index",
    component: lazyLoad('Home')
  },

]

  

标签:vue,componentPath,component,router4,loading,组件,加载
From: https://www.cnblogs.com/MainActivity/p/17356455.html

相关文章

  • 如何实现vue项目中使用Baidu Map,有多个点,鼠标移入出现文字标注,移除消失文字标注,并且点
    直接来案例,再分析;<template> <divref="map"style="height:100vh;"></div></template><script>exportdefault{ data(){  return{   points:[    {lng:116.404,lat:39.915,name:'北京天安门......
  • vue-router学习笔记
    1.路由基础配置 https://router.vuejs.org/zh/guide/2.动态路由根据设置的路径参数实现 constroutes=[//动态字段以冒号开始{path:'/users/:id',component:User},]。需要注意的是参数改变时(第一次访问该路径后,第二次起),组件实例被重复使用,会导致vue的生命周期......
  • vue主应用,qiankun 子应用切换主应用样式错乱问题
    vue主应用,qiankun子应用切换主应用样式错乱问题 constchildRoute=['/flinkdashboard','/dolphinscheduler','/datainsight']//子应用路径constisChildRoute=(path)=>childRoute.some(item=>path.startsWith(item));constrawAppendChild=......
  • cesium-1-加载影像数据和影像数据基础知识
    1、影像数据的图层类有哪些viewer-->imageryLayers(ImageryLayerCollection类型)-->ImageryLayer类型-->ImageryProvider抽象类viewer下有ImageryLayerCollection类型的imageryLayers用来存放影像数据(可多个),只能是ImageryLayer类型变量(包含影像数据但除了影像数据之外还有......
  • vue-cli使用方法
    是什么?vue-clivuecommandlineinterface是vue命令行接口工具怎么用?1.安装npm:【Win7】只支持node.jsv13.14.0或更早1)到官网下载https://nodejs.org/download/release/v13.14.0/node-v13.14.0-x64.msi2)注意在安装node.js时,勾选“Automaticallyinstalltheneces......
  • 若依vue框架添加kotlin依赖,实现kotlin和java混合编译
    第一步:在最外部pom.xml文件添加版本号和依赖管理properties添加版本号<kotlin.version>1.4.21</kotlin.version><!--启用kotlin增量编译--><kotlin.compiler.incremental>true</kotlin.compiler.incremental>dependencyManagement添加依赖管理<!-......
  • vue3 uniapp Uncaught (in promise) TypeError: Cannot read properties of null (rea
    引发这个问题是在三级页面中使用uni.navigateBack({delta:2})返回到一级页面再重一级页面进入二级页面二级页面中引用的组件引发的emitsOptions报错//原因:我在二级页面中的组件使用ts的emit写法引发的报错constemit=defineEmits<{(e:'confirm',contents:string):......
  • vue3中useRouter和useRoute的使用
    vue3路由新玩法useRoute和useRouter详解原文链接原来的vue2路由是通过this.$route和this.$router来控制的。现在vue3有所变化,useRoute相当于以前的this.$route,而useRouter相当于this.$router一、useRouter手动控制路由变化import{useRouter}from'vue-router'exportdef......
  • Vue监听页面放大缩小事件
    Vue监听页面放大缩小事件 ,使用window.addEventListener,methods中方法ChangeWin(){letratio=this.getRatio();letPwidth=window.screen.width*this.getRatio()/100;letPHeight=window.screen.height*this.getRatio()/100;//1920*1080......
  • 动力节点老杜Vue框架教程【三】Vue组件化
    Vue.js是一个渐进式MVVM框架,目前被广泛使用,也成为前端中最火爆的框架Vue可以按照实际需要逐步进阶使用更多特性,也是前端的必备技能动力节点老杜的Vue2+3全家桶教程已经上线咯!学习地址:https://www.bilibili.com/video/BV17h41137i4/视频将从Vue2开始讲解,一步一个案例,知识点......