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

在 Vue 3 的 Composition API 中,如何实现动态组件

在 Vue 3 的 Composition API 中,实现动态组件主要借助 <component> 标签结合 :is 动态绑定属性,同时搭配响应式数据来控制显示不同的组件。以下将从基础实现、加载异步组件以及组件缓存三个方面详细介绍实现动态组件的方法。

基础实现

<template>
  <div>
    <!-- 切换组件的按钮 -->
    <button @click="currentComponent = 'ComponentA'">显示组件 A</button>
    <button @click="currentComponent = 'ComponentB'">显示组件 B</button>
    <!-- 动态组件 -->
    <component :is="currentComponent" />
  </div>
</template>

<script setup>
import { ref } from 'vue';
// 引入需要的组件
import ComponentA from './ComponentA.vue';
import ComponentB from './ComponentB.vue';

// 使用 ref 创建响应式数据来存储当前要显示的组件名
const currentComponent = ref('ComponentA');

// 注册组件,这样动态组件才能识别
const components = {
  ComponentA,
  ComponentB
};
</script>
代码解释:
  1. 导入组件:使用 import 语句将需要动态切换的组件引入。
  2. 创建响应式数据:借助 ref 创建 currentComponent 响应式变量,其初始值为 'ComponentA',用于存储当前要显示的组件名。
  3. 动态组件渲染<component :is="currentComponent" /> 标签中的 :is 绑定 currentComponent,Vue 会根据该变量的值动态渲染对应的组件。
  4. 按钮切换:点击按钮时,通过修改 currentComponent 的值来切换显示的组件。

加载异步组件

<template>
  <div>
    <button @click="currentComponent = 'AsyncComponentA'">显示异步组件 A</button>
    <button @click="currentComponent = 'AsyncComponentB'">显示异步组件 B</button>
    <Suspense>
      <!-- 正常显示的组件 -->
      <template #default>
        <component :is="currentComponent" />
      </template>
      <!-- 加载中的占位组件 -->
      <template #fallback>
        <div>加载中...</div>
      </template>
    </Suspense>
  </div>
</template>

<script setup>
import { ref } from 'vue';
// 使用 defineAsyncComponent 定义异步组件
const AsyncComponentA = defineAsyncComponent(() => import('./AsyncComponentA.vue'));
const AsyncComponentB = defineAsyncComponent(() => import('./AsyncComponentB.vue'));

const currentComponent = ref('AsyncComponentA');

const components = {
  AsyncComponentA,
  AsyncComponentB
};
</script>
代码解释:
  1. 定义异步组件:使用 defineAsyncComponent 函数结合 import() 语法动态导入组件,实现异步加载。
  2. 使用 <Suspense><Suspense> 组件用于处理异步组件的加载状态,#default 插槽用于显示正常加载的组件,#fallback 插槽用于显示加载过程中的占位内容。

组件缓存

若要缓存动态组件的状态,可使用 <KeepAlive> 组件。

<template>
  <div>
    <button @click="currentComponent = 'ComponentA'">显示组件 A</button>
    <button @click="currentComponent = 'ComponentB'">显示组件 B</button>
    <!-- 缓存动态组件 -->
    <KeepAlive>
      <component :is="currentComponent" />
    </KeepAlive>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import ComponentA from './ComponentA.vue';
import ComponentB from './ComponentB.vue';

const currentComponent = ref('ComponentA');

const components = {
  ComponentA,
  ComponentB
};
</script>
代码解释:
  • <KeepAlive> 组件:将 <component> 标签包裹在 <KeepAlive> 中,这样在切换组件时,被隐藏的组件状态会被缓存,再次显示时无需重新初始化。

通过以上方法,你可以在 Vue 3 的 Composition API 中实现动态组件的基础切换、异步加载以及状态缓存等功能。


http://www.kler.cn/a/565488.html

相关文章:

  • 二叉树的概念和静态实现 【复习笔记】
  • C++中map容器常见用法(AI)
  • ISIS(中间系统到中间系统)——基础
  • 跟着deepseek再学习git
  • 常用 ADB 命令汇总
  • JavaScript系列(85)--包管理工具详解
  • Redis开启远程连接
  • 【废物研究生零基础刷算法】DFS与递归(二)习题
  • Ubuntu解决Genesis报错
  • 事件【Qt】
  • elementUI方案汇总
  • 前端面试题---小程序跟vue的声明周期的区别
  • QVariantList使用详解
  • 力扣1557. 可以到达所有点的最少点数目
  • 告别阻塞,迎接高效:掌握 AsyncIOScheduler 实现异步任务调度
  • Visionpro cogToolBlockEditV2.Refresh()
  • Idea 和 Pycharm 快捷键
  • Linux报 “device or resource busy” 异常的原因以及解决办法
  • javaweb将上传的图片保存在项目文件webapp下的upload文件夹下
  • 从像素到光线:现代Shader开发的范式演进与性能优化实践