当前位置: 首页 > news >正文

vue异步导入

vue的异步导入

大家开发vue的时候或多或少路由的异步导入 component: () =>import(“@/views/A_centerControl/intelligent-control/access-user-group”),当然这是路由页面,那么组件也是可以异步导入的

使用方式

组件的异步导入非常简单,主要是一个api :defineAsyncComponent
先看看官方用法

import { defineAsyncComponent } from 'vue'const AsyncComp = defineAsyncComponent(() =>import('./components/MyComponent.vue')
)

用法是比较简单的
那现在我们整个项目来实践一下

区别

首先,我们看看如果不用异步导入的话,页面组件会是什么样子的

<template><div>indexVue</div><sync-index></sync-index>
</template>
<script setup>
import SyncIndex from './component/SyncIndex.vue';
</script>
<style lang="scss" scoped></style>

此时页面显示

image
然后我们切换到异步加载

<template><div>indexVue</div><!-- <sync-index></sync-index> --><SyncIndex></SyncIndex>
</template>
<script setup>
import { defineAsyncComponent } from 'vue';
// import SyncIndex from './component/SyncIndex.vue';
const SyncIndex = defineAsyncComponent(() => import('./component/AsyncIndex.vue'));
</script>
<style lang="scss" scoped></style>

image
看起来没什么区别呀;
确实,在这种使用情况下确实没啥区别,但是,如果这个组件是通过点击某个按钮显示的呢,最经典的就是后台管理项目点击新增的弹框,那这时候我们再看一下

<template><div>indexVue</div><!-- <sync-index></sync-index> --><el-button type="primary" @click="loadingComponent">异步组件</el-button><component:is="currentComponent"v-show="isload":isPlaying@remove="remove"></component>
</template>
<script setup>
import { defineAsyncComponent } from 'vue';
// import SyncIndex from './component/SyncIndex.vue';
const currentComponent = ref();
const isload = ref(false);
const loadingComponent = () => {if (currentComponent.value) {isload.value = true;return;}currentComponent.value = defineAsyncComponent(() => import('./component/AsyncIndex.vue'),);isload.value = true;
};
</script>
<style lang="scss" scoped></style>

上面的代码是当点击的时候会加载组件,并显示组件内容,我们先看看刚进入这个页面
image
有聪明的小伙子已经发现了,AsyncIndex组件再网络请求中没有,那让我们点击按钮看一下结果
image
发现了吧,当点击按钮加载组件的时候才会去获取组件,跟我们以前的写法不一致,import SyncIndex from './component/SyncIndex.vue';这种方式是进入这个页面的时候就会去加载组件,而异步导入是在要被加载的时候才会导入,我们这个案例就是再点击的时候加载,这其实是一种页面的优化手段
如果一个页面有一个比较复杂的弹框或者页面,只有在触发一个事件的时候才会显示,就可以使用异步导入的方式,将这个复杂组件推迟加载

等待组件显示

当网络环境不好的时候,希望有一个等待的过程
image

image

<template><div><h1>异步组件示例</h1><el-button type="primary" @click="loadComponent">加载异步组件</el-button><component :is="AsyncComp" /></div>
</template><script setup>
import { shallowRef, defineAsyncComponent } from 'vue';
import LoadingComponent from './component/Loading.vue'; // 加载组件// 定义异步组件
const AsyncComp = shallowRef();
// // 点击按钮时加载组件
const loadComponent = () => {AsyncComp.value = defineAsyncComponent({loader: () => import('./component/AsyncIndex.vue'), // 异步加载的组件loadingComponent: LoadingComponent, // 加载时显示的组件delay: 20, // 加载延迟});
};
</script><style scoped>
/* 样式 */
</style>

总结

以上就是异步导入defineAsyncComponent的用法,可能会有错误,请各位巨佬提出

http://www.xdnf.cn/news/467983.html

相关文章:

  • 动态库静态加载与动态加载
  • PT100温度传感器应用场景
  • PADS 9.5安装教程
  • 非常详细的HTTP状态码介绍
  • 张 提示词优化(相似计算模式)深度学习中的损失函数优化技巧
  • 当下流行的智能体通信协议:MCP、A2A、ANP 分别是什么?
  • IPage<T> 与 Page<T> 有什么区别?
  • CSS相关知识补充
  • git工具使用详细教程-------命令行和图形化工具
  • MySQL表的操作
  • 2025年长三角高校数模竞赛B题Q1-Q3详细求解与Q4详细分析
  • 镍钯金电路板厂家有哪些?
  • pytest框架 - 第二集 allure报告
  • 雾锁王国开服联机教程-专用服务器
  • 【上位机——WPF】App.xml和Application类简介
  • 【OpenGL学习】(一)创建窗口
  • 不建议在useEffect中进行数据获取的理由
  • 以项目的方式学QT开发(一)——超详细讲解(120000多字详细讲解,涵盖qt大量知识)逐步更新!
  • 【中级软件设计师】网络攻击(附软考真题)
  • cmake 指定安装路径
  • 街景主观感知全流程(自建数据集+两两对比程序+Trueskill计算评分代码+训练模型+大规模预测)11
  • Excel导入日期变数字,数据库入库异常的排查与修复过程
  • 【iOS】alloc的实际流程
  • 【办公类-100-01】20250515手机导出教学照片,自动上传csdn+最大化、最小化Vs界面
  • AD PCB板logo及二维码放置
  • Linux基础 -- 在内存中使用chroot修复eMMC
  • 非加密散列算法的应用-MurmurHash
  • 【Java】Spring IoC中的相关注解
  • 关于mysql分区键
  • Web GIS可视化地图框架Leaflet、OpenLayers、Mapbox、Cesium、ArcGis for JavaScript