vue组件懒加载

vue懒加载 Vue懒加载 阅读详情

1 路由懒加载

路由懒加载常用的两种方式是import(官方推荐)和require

Vue官网路由懒加载开发文档

路由懒加载 | Vue Router

1.1 使用import实现懒加载

router

import Vue from 'vue'
import VueRouter from 'vue-router'
​
Vue.use(VueRouter)
​
/*
// 原始加载方式
import Views from "@/views"
import First from "@/views/First"
import Second from "@/views/Second"
import Third from "@/views/Third"
*/
​
// "/* webpackChunkName:'mycomp' */ "的作用是将文件打包在同一个异步块‘mycomp’中
const Views = ()=>import(/* webpackChunkName:'mycomp' */ "@/views")
const First = ()=>import(/* webpackChunkName:'mycomp' */ "@/views/First")
const Second = ()=>import(/* webpackChunkName:'mycomp' */ "@/views/Second")
const Third = ()=>import(/* webpackChunkName:'mycomp' */ "@/views/Third")
​
​
const router = new VueRouter({
    routes:[
        {
            path:'/',
​
            // 一级导航
            component: Views,
            // 二级导航
            children:[
                {
                    // 重定向到主页
                    path:'/',
                    redirect:'/first'
                },
                {
                    path:'/first',
                    name:'first',
                    component: First,
                    meta:{
                        title:"第一个"
                    }
                },
                {
                    path:'/second',
                    name:'second',
                    component: Second,
                    meta:{
                        title:"第二个"
                    }
                },
                {
                    path:'/third',
                    name:'third',
                    component: Third,
                    meta:{
                        title:"第三个"
                    }
                }
​
            ]
        }
    ]
});
​
// 设置页面标题
router.afterEach((to)=>{
    document.title = to.meta.title;
})
​
export default router;

截图

1.2 使用require实现懒加载

import Vue from 'vue'
import VueRouter from 'vue-router'
​
Vue.use(VueRouter)
​
/*
// 原始加载方式
import Views from "@/views"
import First from "@/views/First"
import Second from "@/views/Second"
import Third from "@/views/Third"
*/
​
// ---* 1 import方法 *---
// "/* webpackChunkName:'mycomp' */ "的作用是将文件打包在同一个异步块‘mycomp’中
// const Views = ()=>import(/* webpackChunkName:'mycomp' */ "@/views")
// const First = ()=>import(/* webpackChunkName:'mycomp' */ "@/views/First")
// const Second = ()=>import(/* webpackChunkName:'mycomp' */ "@/views/Second")
// const Third = ()=>import(/* webpackChunkName:'mycomp' */ "@/views/Third")
​
// ---* 2 require方法 *---
const Views = resolve=>(require(["@/views"],resolve))
const First = resolve=>(require(["@/views/First"],resolve))
const Second = resolve=>(require(["@/views/Second"],resolve))
const Third = resolve=>(require(["@/views/Third"],resolve))
​
const router = new VueRouter({
    routes:[
        {
            path:'/',
​
            // 一级导航
            component: Views,
            // 二级导航
            children:[
                {
                    // 重定向到主页
                    path:'/',
                    redirect:'/first'
                },
                {
                    path:'/first',
                    name:'first',
                    component: First,
                    meta:{
                        title:"第一个"
                    }
                },
                {
                    path:'/second',
                    name:'second',
                    component: Second,
                    meta:{
                        title:"第二个"
                    }
                },
                {
                    path:'/third',
                    name:'third',
                    component: Third,
                    meta:{
                        title:"第三个"
                    }
                }
​
            ]
        }
    ]
});
​
// 设置页面标题
router.afterEach((to)=>{
    document.title = to.meta.title;
})
​
export default router;

截图

2 组件懒加载

组件懒加载一般有两种方法,一种使用component的is属性和computed实现,另一种是使用v-if实现。

Vue官网动态加载组件开发文档

动态组件 & 异步组件 — Vue.js

2.1 页面切换懒加载

在单组件注册中并考虑缓存机制,页面切换懒加载主要有两种实现方式,一种是使用computed、component的is属性、import,另一种是使用computed、component的is属性和require。

<template>
  <div id="first">
   
    <div class="fir_title">
      First Page
      河南大学
    </div>
​
    <div class="fir_nav">
      <button v-on:click='switchPage("first")'>FirCom-1</button>
      <button v-on:click='switchPage("second")'>FirCom-2</button>
      <button v-on:click='switchPage("third")'>FirCom-3</button>
    </div>
  
    <div class="fir_con">
      <component v-bind:is='getFirstCom'></component>
    </div>
​
  </div>
</template>
​
<script>
​
/*
// ---* 1 使用标签属性is和import *---
const FirstComFirst = ()=>import("./FirstComFirst")
const FirstComSecond = ()=>import("./FirstComSecond")
const FirstComThird = ()=>import("./FirstComThird")
*/
​
// ---* 2 使用标签属性is和require *---
const FirstComFirst = resolve=>(require(["./FirstComFirst"],resolve))
const FirstComSecond = resolve=>(require(["./FirstComSecond"],resolve))
const FirstComThird = resolve=>(require(["./FirstComThird"],resolve))
​
​
export default {
  name: 'First',
  components: {
    
  },
  data: function(){
    return{
      curPage: FirstComFirst
    }
  },
​
  methods:{
    switchPage: function(page){
      switch(page){
        case 'first':{
          this.curPage = FirstComFirst;
          break;
        }
        case 'second':{
          this.curPage = FirstComSecond;
          break;
        }
        case 'third':{
          this.curPage = FirstComThird;
          break;
        }
      }
    }
  },
​
  computed:{
    // 借助计算属性
    getFirstCom: function(){
      return this.curPage;
    }
  }
}
</script>
​
<style scope lang="less">
​
.fir_title{
  font-weight: bold;
}
​
.fir_nav>button{
  margin: 10px 6px;
}
​
.fir_con{
  margin-top: 20px;
}
​
</style>

2.2 使用if实现需要时加载

<template>
  <div id="second">
    <div class="sec_title">
      Second Page
      河南大学
    </div>
    <div class="sec_nav">
      <button v-on:click="showFirst()">显示第一个组件</button>
      <button v-on:click="showSecond()">显示第二个组件</button>
    </div>
    <div class="sec_com">
      <div>第一个组件</div>
      <component v-if="isFirstShow" v-bind:is="SecComFirst"></component>
      <SecComFirstTag v-if="isFirstShow"></SecComFirstTag>
    </div>
    <div>
      <div>第二个组件</div>
      <component v-if="isSecondShow" v-bind:is="SecComSecond"></component>
    </div>
  </div>
</template>
​
<script>
​
// 传统方法导入
import SecComFirstTag from "./SecComFirst.vue"
export default {
  name: 'Second',
  components: {
    SecComFirstTag
  },
  data: function(){
    return{
      // Import方法导入
      SecComFirst: ()=>import("./SecComFirst"),
      SecComSecond: ()=>import("./SecComSecond"),
​
      isFirstShow: false,
      isSecondShow: false
    }
  },
​
  methods:{
    showFirst: function(){
      if(this.isFirstShow){
        this.isFirstShow = false;
      }else{
        this.isFirstShow = true;
      }
    },
    showSecond: function(){
      this.isSecondShow = this.isSecondShow?false:true;
    }
  }
}
</script>
​
<style scope lang="less">
.sec_title{
  font-weight: bold;
}
.sec_nav>button{
  margin: 10px 6px;
}
.sec_com{
  margin: 10px 0px;
}
</style>
​

2.3 两种import导入

两种import方式导入组件。

<template>
  <div id="third">
    <div class="th_title">
      Third page 河南大学
    </div>
    <div class="th_com">
      <component v-bind:is="ThirdComFirst"></component>
      <ThirdComFirstTag></ThirdComFirstTag>
    </div>
​
     <div class="th_com">
      <component v-bind:is="ThirdComSecond"></component>
      <ThirdComSecondTag></ThirdComSecondTag>
    </div>
    
  </div>
</template>
​
<script>
​
// 传统方法导入
import ThirdComFirstTag from "./ThirdComFirst"
import ThirdComSecondTag from "./ThirdComSecond"
​
// 下面的方法和传统的方法相似
​
​
export default {
  name: 'Views',
  components: {
    ThirdComFirstTag,
    ThirdComSecondTag
  },
​
  data: function(){
    return{
      ThirdComFirst: ()=>import("./ThirdComFirst"),
      ThirdComSecond: ()=>import("./ThirdComSecond")
    }
  }
}
</script>
​
<style scope lang="less">
​
.th_title{
  font-weight: bold;
}
​
.th_com{
  margin: 10px 0px;
}
​
</style>
​

3 全部页面代码

3.1 main.js

import Vue from 'vue'
import App from './App.vue'
import router from '@/router'
​
Vue.config.productionTip = false
​
new Vue({
  render: h => h(App),
  router
}).$mount('#app')

3.2 router

index.js

import Vue from 'vue'
import VueRouter from 'vue-router'
​
Vue.use(VueRouter)
​
/*
// 原始加载方式
import Views from "@/views"
import First from "@/views/First"
import Second from "@/views/Second"
import Third from "@/views/Third"
*/
​
// ---* 1 import方法 *---
// "/* webpackChunkName:'mycomp' */ "的作用是将文件打包在同一个异步块‘mycomp’中
// const Views = ()=>import(/* webpackChunkName:'mycomp' */ "@/views")
// const First = ()=>import(/* webpackChunkName:'mycomp' */ "@/views/First")
// const Second = ()=>import(/* webpackChunkName:'mycomp' */ "@/views/Second")
// const Third = ()=>import(/* webpackChunkName:'mycomp' */ "@/views/Third")
​
// ---* 2 require方法 *---
const Views = resolve=>(require(["@/views"],resolve))
const First = resolve=>(require(["@/views/First"],resolve))
const Second = resolve=>(require(["@/views/Second"],resolve))
const Third = resolve=>(require(["@/views/Third"],resolve))
​
const router = new VueRouter({
    routes:[
        {
            path:'/',
​
            // 一级导航
            component: Views,
            // 二级导航
            children:[
                {
                    // 重定向到主页
                    path:'/',
                    redirect:'/first'
                },
                {
                    path:'/first',
                    name:'first',
                    component: First,
                    meta:{
                        title:"第一个"
                    }
                },
                {
                    path:'/second',
                    name:'second',
                    component: Second,
                    meta:{
                        title:"第二个"
                    }
                },
                {
                    path:'/third',
                    name:'third',
                    component: Third,
                    meta:{
                        title:"第三个"
                    }
                }
​
            ]
        }
    ]
});
​
// 设置页面标题
router.afterEach((to)=>{
    document.title = to.meta.title;
})
​
export default router;

3.3 App.vue

<template>
  <div id="app">
    <router-view></router-view>
  </div>
</template>
​
<script>
​
export default {
  name: 'App',
  components: {
  }
}
</script>
​
<style scope lang="less">
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>
​

3.4 views

index.vue

<template>
  <div id="views">
    <div class="my_nav">
      <router-link to="/first">第一个页面</router-link>
      <router-link to="/second">第二个页面</router-link>
      <router-link to="/third">第三个页面</router-link>
    </div>
    <div class="my_page">
      <router-view></router-view>
    </div>
    
  </div>
</template>
​
<script>
​
export default {
  name: 'Views',
  components: {
    
  }
}
</script>
​
<style scope lang="less">
.my_nav{
  height: 40px;
}
.my_nav>a{
  margin: 0 8px
}
​
.my_page{
  margin-top: 20px;
}
​
</style>
​

3.5 First

index.vue

<template>
  <div id="first">
   
    <div class="fir_title">
      First Page
      河南大学
    </div>
​
    <div class="fir_nav">
      <button v-on:click='switchPage("first")'>FirCom-1</button>
      <button v-on:click='switchPage("second")'>FirCom-2</button>
      <button v-on:click='switchPage("third")'>FirCom-3</button>
    </div>
  
    <div class="fir_con">
      <component v-bind:is='getFirstCom'></component>
    </div>
​
  </div>
</template>
​
<script>
​
/*
// ---* 1 使用标签属性is和import *---
const FirstComFirst = ()=>import("./FirstComFirst")
const FirstComSecond = ()=>import("./FirstComSecond")
const FirstComThird = ()=>import("./FirstComThird")
*/
​
// ---* 2 使用标签属性is和require *---
const FirstComFirst = resolve=>(require(["./FirstComFirst"],resolve))
const FirstComSecond = resolve=>(require(["./FirstComSecond"],resolve))
const FirstComThird = resolve=>(require(["./FirstComThird"],resolve))
​
​
export default {
  name: 'First',
  components: {
    
  },
  data: function(){
    return{
      curPage: FirstComFirst
    }
  },
​
  methods:{
    switchPage: function(page){
      switch(page){
        case 'first':{
          this.curPage = FirstComFirst;
          break;
        }
        case 'second':{
          this.curPage = FirstComSecond;
          break;
        }
        case 'third':{
          this.curPage = FirstComThird;
          break;
        }
      }
    }
  },
​
  computed:{
    // 借助计算属性
    getFirstCom: function(){
      return this.curPage;
    }
  }
}
</script>
​
<style scope lang="less">
​
.fir_title{
  font-weight: bold;
}
​
.fir_nav>button{
  margin: 10px 6px;
}
​
.fir_con{
  margin-top: 20px;
}
​
​
</style>

​

FirstComOne.vue

<template>
  <div>
    First component one
  </div>
</template>
​
<script>
​
export default {
  name: 'FirstComOne',
  components: {
    
  }
}
</script>
​
<style scope lang="less">
​
</style>
​

FirstComSecond.vue

<template>
  <div>
    First component second
  </div>
</template>
​
<script>
​
export default {
  name: 'FirstComSecond',
  components: {
    
  },
​
  created: function(){
    console.log("FirstComSecond");
  }
}
</script>
​
<style scope lang="less">
​
</style>
​

FirstComThird.vue

<template>
  <div>
    First component third
  </div>
</template>
​
<script>
​
export default {
  name: 'FirstComThird',
  components: {
    
  },
​
  created: function(){
    console.log("FirstComThird");
  }
}
</script>
​
<style scope lang="less">
​
</style>
​

3.6 Second

index.vue

<template>
  <div id="second">
    <div class="sec_title">
      Second Page
      河南大学
    </div>
    <div class="sec_nav">
      <button v-on:click="showFirst()">显示第一个组件</button>
      <button v-on:click="showSecond()">显示第二个组件</button>
    </div>
    <div class="sec_com">
      <div>第一个组件</div>
      <component v-if="isFirstShow" v-bind:is="SecComFirst"></component>
      <SecComFirstTag v-if="isFirstShow"></SecComFirstTag>
    </div>
    <div>
      <div>第二个组件</div>
      <component v-if="isSecondShow" v-bind:is="SecComSecond"></component>
    </div>
  </div>
</template>
​
<script>
​
// 传统方法导入
import SecComFirstTag from "./SecComFirst.vue"
export default {
  name: 'Second',
  components: {
    SecComFirstTag
  },
  data: function(){
    return{
      // Import方法导入
      SecComFirst: ()=>import("./SecComFirst"),
      SecComSecond: ()=>import("./SecComSecond"),
​
      isFirstShow: false,
      isSecondShow: false
    }
  },
​
  methods:{
    showFirst: function(){
      if(this.isFirstShow){
        this.isFirstShow = false;
      }else{
        this.isFirstShow = true;
      }
    },
    showSecond: function(){
      this.isSecondShow = this.isSecondShow?false:true;
    }
  }
}
</script>
​
<style scope lang="less">
.sec_title{
  font-weight: bold;
}
.sec_nav>button{
  margin: 10px 6px;
}
.sec_com{
  margin: 10px 0px;
}
</style>

截图
​

SecComFirst.vue

<template>
  <div>
   Second component First
  </div>
</template>
​
<script>
​
export default {
  name: 'SecComFirst',
  components: {
    
  },
​
  created: function(){
    console.log("Second component Frist !")
  }
}
</script>
​
<style scope lang="less">
​
</style>
​

SecComSecond.vue

<template>
  <div>
    Second component Second
  </div>
</template>
​
<script>
​
export default {
  name: 'SecComSecond',
  components: {
    
  },
​
  created: function(){
    console.log("Second component second !")
  }
}
</script>
​
<style scope lang="less">
​
</style>
​

3.7 Third

index.vue

<template>
  <div id="third">
    <div class="th_title">
      Third page 河南大学
    </div>
    <div class="th_com">
      <component v-bind:is="ThirdComFirst"></component>
      <ThirdComFirstTag></ThirdComFirstTag>
    </div>
​
     <div class="th_com">
      <component v-bind:is="ThirdComSecond"></component>
      <ThirdComSecondTag></ThirdComSecondTag>
    </div>
    
  </div>
</template>
​
<script>
​
// 传统方法导入
import ThirdComFirstTag from "./ThirdComFirst"
import ThirdComSecondTag from "./ThirdComSecond"
​
// 下面的方法和传统的方法相似
​
​
export default {
  name: 'Views',
  components: {
    ThirdComFirstTag,
    ThirdComSecondTag
  },
​
  data: function(){
    return{
      ThirdComFirst: ()=>import("./ThirdComFirst"),
      ThirdComSecond: ()=>import("./ThirdComSecond")
    }
  }
}
</script>
​
<style scope lang="less">
​
.th_title{
  font-weight: bold;
}
​
.th_com{
  margin: 10px 0px;
}
​
</style>
​

ThirdComFirst.vue

<template>
  <div>
   Third component First
  </div>
</template>
​
<script>
​
export default {
  name: 'ThirdComFirst',
  components: {
    
  },
​
  created: function(){
    console.log("Third component Frist !")
  }
}
</script>
​
<style scope lang="less">
​
</style>
​

ThirdComSecond.vue

<template>
  <div>
    Third component Second
  </div>
</template>
​
<script>
​
export default {
  name: 'ThirdComSecond',
  components: {
    
  },
​
  created: function(){
    console.log("Third component second !")
  }
}
</script>
​
<style scope lang="less">
​
</style>
​

4 import和require的区别

4.1 import/export

来源于ECMAScript2015(ES6),node支持,部分高版本的原生浏览器支持import/export(需要加type="module" 属性),脚本静态编译。

4.2 require/exports

来源于CommonJS,node支持,原生浏览器不支持 require/exports,脚本运行时动态加载对象。

vue2项目中常用的两种数据懒加载方式 前端项目优化分为懒加载和预加载1.懒加载顾名思义就是延迟加载,项目中常见的就是图片懒加载比如滚动条触底加载,虚拟列表,包括我们经常用的延时器加载数据,大家面试经常被问到后端返回十万条数据前端怎么渲染?这也是懒加载的内容。在项目中我们经常会遇到前后端通过websocket通信,数据会返回的很快,如果一下全部渲染出来必然会造成浏览器堵塞,前端页面卡顿,渲染不流畅等问题。这时候我们就用到了数据懒加载。我们首页图片比较多的时候会用到图片懒加载和滚动条触底加载这两种方式。 阅读详情

相关推荐

Vue路由懒加载和组件懒加载

一、什么是懒加载? 懒加载简单来说就是延迟加载或按需加载。 二、懒加载类型 常用的懒加载方式有两种:vue异步组件 和 ES的import加载 (1)路由懒加载 异步组件懒加载: component:resolve=>(require(['需要加载的路由的地址']),resolve) import Vue from 'vue' import Router from 'vue-router' Vue.use(Router) export default new Router({ routes:

轻风细雨_林木木 1031

Vue组件懒加载

本文将介绍如何在Vue 2和Vue 3中实现组件懒加载和一些使用场景,附有案例

qq_54334713的博客 5213

Cesium生态盘点:超图、火星3D等15个二次开发框架对比

本文深入解析Cesium生态中的15个二次开发框架,包括超图、火星3D等主流选择,帮助开发者根据项目需求进行精准选型。从性能表现、功能完备性、开发友好度等维度进行对比,特别适合需要处理地理空间数据和构建三维可视化的JavaScript开发者。

weixin_28197941的博客 497

vue子组件懒加载(按需加载)

vue子组件懒加载(按需加载)

m0_68956554的博客 2002

华为、Cisco、H3C、中兴网络设备如何配置syslog日志采集

华为、Cisco、H3C、中兴网络设备如何配置syslog日志采集

weixin_40239644的博客 8706

【2024保研经验帖】中科院信工所夏令营经验分享

中科院信工所夏令营经验分享,信工所夏令营据说只要报了就能进,这个是不是真的尚不清楚,不过qq群里有八九百个人,也属实可怕。招生老师说我们这届夏令营招200-300个左右,于是又开始惶惶不安。 夏令营时期都是线上进行,先是每个科室进行线上招生宣讲,然后报名第一志愿部门和第二志愿部门,不同部门的考核是不同的。有的部门只要求英语面试和综合面试,有的部门除了面试还需要机试,而有的部门除了这些还需要笔试。

m0_63002183的博客 4489

Vue 懒加载

Vue 懒加载

我是 二川兄,对Web开发、GIS开发、3D模型、机器学习、面试技巧等方面都有一些涉猎~ 欢迎您加入技术交流圈!你可以在我的文章末尾找到我~ 7553

【WILEY】从Revision到Online:逐层拆解投稿状态更新与关键文件提交实战

本文详细解析了学术论文从返修到在线发表的全过程,重点拆解了投稿系统状态更新背后的含义与应对策略。文章提供了返修稿与回复信的准备技巧、关键补充文件(如图文摘要)的提交要点,并分享了校对校样与处理出版协议的实战经验,帮助作者高效、专业地完成发表流程。

weixin_29209805的博客 713

vue项目中路由懒加载及组件懒加载

vue路由懒加载及组件懒加载 一、为什么要使用路由懒加载 为给客户更好的客户体验,首屏组件加载速度更快一些,解决白屏问题。 二、定义 懒加载简单来说就是延迟加载或按需加载,即在需要的时候的时候进行加载。 三、路由懒加载使用 常用的懒加载方式有两种:即使用vue异步组件 和 ES中的import 1、未使用懒加载,vue中的代码如下 import Vue from 'vue' import Router from 'vue-router' import HelloWorld from '@/componen

weixin_45290022的博客 1189

触摸延时灯课程设计

Multisim源文件下载: 实验名称:­­­­­­­­­­­­­­­­­­­ 课程设计1:触摸延时灯仿真设计 设计要求: 利用单稳态触发器设计触控延时灯,要求利用触发波形的边沿模拟触控点实现触发,触发后3秒灯亮,灯可以亮的时间可在5-10秒之间可调。 设计思路: 利用集成单稳态触发器CD4098设计简易的触摸延时灯电路。应用CD4098可以实现脉冲延时,原理图如下。4脚未触发信号输入端,可通过比较器对触摸产生的信号进行比较后再产生触发信号,使触发准确度得以提高。...

m0_54240221的博客 1万+

手把手教你学基于 Linux 的 NPU 固件开发-第 10 讲:可靠性设计(RAS)——让系统坚如磐石错误检测、纠正与上报机制(ECC、Parity)

组件推荐保护机制备注HBM/DDRScrubbing必须开启,定期巡检片上 SRAMECC(权重/激活值),Parity(指令)权衡面积与可靠性计算单元ABFT或DMR(关键路径)防止计算逻辑错误片上网络CRC重传保证数据传输完整性系统软件AER 上报CE 监控自动恢复闭环管理,杜绝 SDC运维策略预测性维护Checkpoint变被动为主动核心心法(信任硬件,但必须验证。在纳米尺度下,错误是必然发生的物理现象。

MHD0815的博客 91

Vue组件懒加载和路由懒加载

Vue组件懒加载和Vue路由懒加载 这里只记录了router层面上的懒加载方法,还有在*.vue中各种懒加载的方法,例如:需要的时候import组件,用完destory等等。 Vue组件的懒加载 vue-router配置路由 , 使用vue的异步组件技术 , 可以实现按需加载 . 但是,这种情况下一个组件生成一个js文件 router.js import Vue from 'vue' import...

marko_zheng的博客 1370

将Windbg附加到软件进程上排查异常闪退的问题

使用Windbg和Process Explorer排查因为内存泄漏导致的软件异常闪退问题。

dvlinker的技术专栏 1万+

React和Vue组件的懒加载

懒加载,也可以成为按需加载,即页面、数据或者组件在需要加载的时候才进行加载,在开始只加载需要的最少的静态资源和数据信息,尽快的展示首屏页面。 现在先不谈技术,谈谈为什么需要按需加载。有调查显示:页面的加载速度会影响客户的使用体验,加载时间每增加1s,可能都会造成一定比例的客户流失。具体的调查结果和详细数据这里不表,这应该是一个公认的事实了。 现在考虑下面两种情况: 1、网站在开始加载了所有请求,从...

许保殿的博客 1728

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

深入浅出 Vue2 和 Vue3 中的异步组件与懒加载

随着现代前端应用的复杂性增加,性能优化成为了开发者关注的重点之一。异步组件和懒加载(Lazy Loading)是目前最常见的性能优化技术之一,通过将组件按需加载,能够显著减小初次加载的包体积,从而提高用户体验。在 Vue.js 中,异步组件不仅能够有效提升页面加载速度,还能提升应用的响应性。本文将详细讲解 Vue2 和 Vue3 中如何使用异步组件以及懒加载,并对比两者的异同之处,帮助你更好地理解异步组件的实现方式以及如何在项目中优化其使用。

一名热衷于技术的全栈开发者,专注于前端与后端的全面技术探索。在这里,我将分享我在技术领域的学习与成长,助力更多开发者的进步。 1759

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

vue路由懒加载和组件懒加载

懒加载:又叫延迟加载,首页优化需要用到 路由懒加载的2种方式 第一种: 前面不需要import这个组件 const router=new Router({ routes:[ { path:'', name:'lazy', component:resolve=>(require(["@/co...

死亡的挽歌的博客 720

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

vue懒加载和子组件懒加载有什么区别

在这个例子中,我们使用了Vue的异步组件来实现子组件懒加载。在模板中,我们使用了v-if指令来控制组件的显示和隐藏,只有在isComponentLoaded为true时才会显示组件。在模板中,我们使用了v-lazy指令来指定图片的src属性,该指令会在图片进入视口范围时自动加载图片。我们也可以在Vue懒加载的配置中指定预加载、加载中、加载失败等状态的图片。总之,Vue懒加载和子组件懒加载都是为了优化性能和提高用户体验的技术手段,需要根据具体的应用场景选择适合的实现方式。

weixin_42095178的博客 747

Vue 95 ,Vue3 组件懒加载,页面显示 [object Promise]

⭐在现代前端开发领域,性能优化是项目成功的关键因素之一。伴随 Vue 项目规模持续扩大,组件数量不断增多,打包后的文件体积也会显著增大,这会导致应用加载时间延长,严重影响用户体验。Vue3 提供的组件懒加载功能,能够有效解决这一问题,通过在需要时才加载组件,减少初始加载的文件大小,进而提升应用的响应速度。这里将全面且深入地介绍 Vue3 组件懒加载的实现方式,同时探讨相关注意事项。

AI Agent 全栈架构师(AI + GIS + Backend)& 数字芯片系统架构师(FPGA),NorthCity Smile的博客(NcSmile,博主域名:https://ncmile.blog.youkuaiyun.com) 2358

Vue路由懒加载及组件懒加载

一、为什么要使用路由懒加载 为给客户更好的客户体验,首屏组件加载速度更快一些,解决白屏问题。 二、定义 懒加载简单来说就是延迟加载或按需加载,即在需要的时候的时候进行加载。 三、使用 常用的懒加载方式有两种:使用vue异步组件 和 ES中的import 1、未用懒加载,vue中路由代码如下 import Vue from 'vue' import Router from 'vue-router' import HelloWorld from '@/components/HelloWorl.

why404 1692

如何在Vue中实现组件懒加载?这有什么好处?

组件懒加载是一种按需加载模块或组件的技术。简单来说,当用户访问应用程序时,只有那些立即需要的组件会被加载,而其他组件则在真正被需要的时候再加载。这样做可以有效减少首次加载的资源大小,提高应用的性能。

有我更精彩的博客 1539

vue实现路由懒加载及组件懒加载的方式

Vue路由懒加载

qq_42403643的博客 1386

Vue学习笔记 —— vue路由懒加载及组件懒加载

vue路由懒加载及组件懒加载 vue路由懒加载 为什么要使用路由懒加载?为给客户更好的客户体验,首屏组件加载速度更快一些,解决白屏问题。 常用的懒加载方式有两种: 使用vue异步组件 component:resolve=>(['需要加载的路由的地址',resolve]) 使用ES6中的import const HelloWorld = ()=>import('需要加载的模块地址') 1、未用懒加载,vue中的路由代码如下: import Vue from 'vue' import

weixin_43340295的博客 297

如何在 Vue 3 中实现懒加载组件?

懒加载(Lazy Loading)是一种设计模式,它延迟加载资源(如组件、图片等),直到需要时再加载。这种方法可以有效地提高页面首次加载速度,减少不必要的下载,从而增强用户体验。通过以上的步骤,我们成功地在 Vue 3 中实现了懒加载组件。这种方法不仅提升了应用的性能,还优化了用户的体验,让他们在需要的时候才加载组件。懒加载组件是现代前端开发中不可或缺的一部分,通过使用 Vue 3 的异步组件 API,你可以轻松地将这项技术融入你的应用中。

有我更精彩的博客 2445

一文详解VUE组件懒加载

想深入了解 Vue3 中的组件懒加载(异步组件),我会从它和路由懒加载的区别、核心原理、具体用法到进阶场景,一步步讲清楚,让你彻底掌握组件级别的按需加载。

weixin_41413777的博客 807

【Vue3】组件数据懒加载

电商类网站,尤其是首页,内容有好几屏,而如果一上来就加载所有屏的数据,并渲染所有屏的内容会导致首页加载很慢。添加了ref类型提示:MaybeElementRef -> 暴露出去的taget如果赋值类型不对会进行提示。首页中,很多地方都应该使用组件数据懒加载这个功能,不管是哪个模块使用,下面代码都会重复书写。数据懒加载:等组件正式进入到可视区中时,才把组件内部的ajax请求发起,否则不请求数据。目标:封装组件数据懒加载可复用的逻辑。(1)封装通用的懒加载数据api。给ref添加组件类型。

一个前端开发者 2785
上一篇: 基于python使用CV裁剪图片
下一篇: 微服务集群简单搭建(Eureka)
MasonYyp
博客等级 码龄9年 301粉丝 297原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值