vue3的动态组件

动态组件可以根据响应式变量动态的渲染不同的组件,目前是通过Vue 的 元素和特殊的 is attribute 实现的:

<component :is="tabs[currentTab]"></component>

:is 的值可以是以下几种:

被注册的组件名
导入的组件对象
你也可以使用 is attribute 来创建一般的 HTML 元素。

当使用 来在多个组件间作切换时,被切换掉的组件会被卸载。我们可以通过 组件强制被切换掉的组件仍然保持“存活”的状态。

如下
app.vue

<script setup lang="ts">
import HelloWorld from './components/HelloWorld.vue'
import Demo from './components/Demo.vue'
import Child from './components/Child.vue';
import { provide, ref,reactive,shallowRef } from 'vue';
const title = ref("hello")
provide("title", title)

const current=shallowRef(Child)



</script>

<template>

  <div class="wrapper">

    <component :is="current" :title="123"></component>
    <label><input type="radio" v-model="current" :value="Child" /> A</label>
    <label><input type="radio" v-model="current" :value="HelloWorld" /> B</label>

  </div>

</template>

<style scoped></style>

通过点击radio改变current的值从而触发不同的组件渲染。
在这里插入图片描述

在这里插入图片描述

相关推荐

  1. Vue3中使用动态组件

    2024-04-06 23:40:03       68 阅读
  2. Vue3前端框架:动态组件详解

    2024-04-06 23:40:03       50 阅读
  3. Vue3使用component动态展示组件

    2024-04-06 23:40:03       39 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-06 23:40:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-06 23:40:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-06 23:40:03       87 阅读
  4. Python语言-面向对象

    2024-04-06 23:40:03       96 阅读

热门阅读

  1. Docker in Docker原理与实战

    2024-04-06 23:40:03       37 阅读
  2. 移动点的函数

    2024-04-06 23:40:03       43 阅读
  3. 使用神经网络识别病毒序列

    2024-04-06 23:40:03       39 阅读
  4. cmake学习笔记2

    2024-04-06 23:40:03       40 阅读
  5. 渗透测试、人肉搜索算不算犯罪?

    2024-04-06 23:40:03       38 阅读
  6. RabbitMQ死信队列

    2024-04-06 23:40:03       34 阅读
  7. react组件:strictmode

    2024-04-06 23:40:03       39 阅读
  8. 全错排列c++代码

    2024-04-06 23:40:03       35 阅读
  9. 2024.3.23力扣每日一题——统计桌面上的不同数字

    2024-04-06 23:40:03       38 阅读
  10. 《深度学习的数学基础》小结

    2024-04-06 23:40:03       42 阅读