异步组件

@Async
Overview
异步组件 Async Components:仅仅需要的时候才加载 - only load a component from the server when it's needed
使用宏 defineAsyncComponent() 定义异步组件
提高"首屏加载"速度
动态组件如果高度不一致,切换时页面容易抖动;如果不考虑加载性能,建议使用普通组件实现
[] 标签页 - 切换时查看各组件的生命周期函数和网络加载情况
更多案例:标签页/手风琴古诗欣赏详情页轮播图一"鹿"有你
<button v-for="(_, tab) in tabs" @click="currentTab = tab" :key="tab"
:class="['tab-button', { active: currentTab === tab }]">{{ tab }}</button>

<component :is="tabs[currentTab]" class="tab"></component>
import { defineAsyncComponent, ref } from 'vue'

const AsyncHome = defineAsyncComponent(() => import('./components/Home.vue'))
const AsyncInfo = defineAsyncComponent(() => import('./components/Info.vue'))
const AsyncTeam = defineAsyncComponent(() => import('./components/Team.vue'))
const AsyncWork = defineAsyncComponent(() => import('./components/Work.vue'))
   
const currentTab = ref('home')
const tabs = {
    "home": AsyncHome,
    "info": AsyncInfo,
    "team": AsyncTeam,
    "work": AsyncWork
}
Homework
[] 使用条件渲染,在开发者视图 → 网络 Network 中查看异步组件的加载情况,并观察项目发布后的打包情况
[] 首页 HomeView.vue → tabcard:随享瑞幸和颜值水杯
分别体验普通组件和异步组件的开发效果
随享瑞幸
颜值水杯