Vue3使用动态组件 Component

component是vue内置组件,主要作用是 动态渲染组件。本文主要介绍vue内置动态组件component的使用。

目录

一、动态组件的概念

二、使用场景

1、tab栏的切换

2、条件性地渲染组件

3、动态切换组件

4、异步加载组件

5、与路由结合使用

三、使用示例


一、动态组件的概念

多个组件通过component标签挂载在同一个组件中,通过触发动作进行动态切换,常搭配<keep-alive></keep-alive>使用。

二、使用场景

多用于以下几个场景:

1、tab栏的切换

管理系统中切换不同的菜单,展示tab,切换tab可以渲染不同组件,一般搭配<keep-alive></keep-alive>使用。

2、条件性地渲染组件

根据某个条件决定渲染哪个组件。通过在<component>元素上使用v-if指令来实现。

3、动态切换组件

根据用户的交互或状态变化,切换显示不同的组件。通过在<component>元素上使用is属性来指定要渲染的组件。

4、异步加载组件

当组件非常大或需要懒加载时,可以使用动态组件来异步加载组件,从而提高页面加载速度。

5、与路由结合使用

在路由配置中使用动态组件,根据不同的路由路径加载相应的组件。

三、使用示例

1、挂载组件

通过vue的defineAsyncComponent实现挂载组件

const CourseData = defineAsyncComponent(() => import("@/components/Chart/courseData.vue"));

2、component的is属性

<component :is="item.component" />

3、动态组件传值

动态组件的传值遵循基本组件传值规则,除了支持v-bind传值以外,还支持ref引用传值;使用引用传值需要注意的是,需要确定组件之后,再使用ref属性进行传值,否则将会无法获取应用组件的属性。使用v-bind传值代码如下所示:

<template>
    <div>
        <component :is="item.component" :data="reportData" :exam-data="exampData"/>
    </div>
</template>
<script lang="ts" setup>
const CourseData = defineAsyncComponent(() => import("@/components/Chart/courseData.vue"));
const item = reactive({
    component: CourseData
})

const reportData = ref("aaaaa")
const exampData = ref("bbbb")
</script>

4、动态组件数据缓存

若是数据需要动态渲染,组件切换之后会导致之前获得的数据丢失,这个时候,若我们想要在组件切换过程中保持这些组件的状态,避免重复渲染导致性能问题,则可以使用<keep-alive></keep-alive>包裹动态组件,来缓存组件中的数据:

<template>
  <div>
    <div id="dynamic-component-demo" class="demo">
      <button
        v-for="tab in tabs"
        :key="tab"
        :class="['tab-button', { active: currentTab === tab }]"
        @click="currentTab = tab"
      >
        {{ tab }}
      </button>
      <keep-alive>
        <component
          :is="item.component"
          class="tab"
        ></component>
      </keep-alive>
    </div>
  </div>
</template>

四、总结

本篇文章就介绍到这里了,希望对你有帮助!

  • 5
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3中,可以使用 `v-bind` 指令的 `.sync` 修饰符来实现父组件与子组件之间的双向绑定,从而获取子组件实例并调用子组件的方法。 首先,需要在父组件使用 `v-bind` 指令将子组件的实例绑定到父组件的一个变量上。例如: ```vue <template> <div> <component :is="selectedComponent" :child-instance.sync="childInstance"></component> <button @click="handleClick">Call Child's Method</button> </div> </template> <script> import ChildComponent1 from './ChildComponent1.vue' import ChildComponent2 from './ChildComponent2.vue' export default { components: { ChildComponent1, ChildComponent2 }, data() { return { selectedComponent: 'ChildComponent1', childInstance: null } }, methods: { handleClick() { if (this.childInstance) { this.childInstance.doSomething() } } } } </script> ``` 在子组件中,需要使用 `emit` 方法将子组件的实例暴露给父组件。例如: ```vue <template> <div> <p>{{ message }}</p> </div> </template> <script> export default { data() { return { message: 'Hello from ChildComponent1!' } }, mounted() { this.$emit('update:childInstance', this) }, methods: { doSomething() { console.log('ChildComponent1 is doing something...') } } } </script> ``` 这样,父组件就可以在 `childInstance` 变量中获取到子组件的实例,并调用子组件的方法。需要注意的是,这种方式只适用于动态组件,即使用 `v-bind` 指令的 `is` 属性来动态切换组件。如果是静态组件,可以直接通过 `ref` 获取子组件实例。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值