当前位置: 代码网 > it编程>编程语言>Javascript > Vue3动态组件component不生效问题解决方法

Vue3动态组件component不生效问题解决方法

2024年09月08日 Javascript 我要评论
问题:vue3循环渲染动态组件component不生效,页面空白在vue3使用component动态组件展示组件时,组件就是不展示显示空白。在vue2中使用动态变量component展示组件都是没问题

问题: vue3循环渲染动态组件component不生效,页面空白

在vue3使用component动态组件展示组件时,组件就是不展示显示空白。在vue2中使用动态变量component展示组件都是没问题。试了很多方法 踩了很多坑,所以记录下:

  <div class="preview-list" id="canvas-area">
    <component
      v-for="component in components" 
      :key="component.id"
      :is="component.name"
      v-bind="component.props" 
      />
  </div>
<script setup lang="ts">
import ltext from '@/components/ltext'
import { ref } from 'vue'
interface styleprops = {
   text: string;
   fontsize: string;
}
interface componentdata = {
  id: number;
  name: string;
  props?: styleprops;
}
const components = ref<componentdata[]>([
    { id: 1, name: 'ltext', props: { text: 'hello', fontsize: '12px'}},
    { id: 2, name: 'ltext', props: { text: 'hello2', fontsize: '14px'}},
    { id: 3, name: 'ltext', props: { text: 'hello3', fontsize: '16px'}}
])
</script>

因为vue3使用的是setup语法,组件只要import导入就行 不需要再像vue2中在components挂载,这样导致我想渲染的组件是没有渲染出来页面出现空白,尝试了很多办法对应的组件里面添加多个script指定对应的组件名,还是没生效

解决方法

使用shallowreactive或者shallowref把对应的组件名称重新定义下,遍历component时,is采用对象key获取对应的对应的组件,这样组件就显示出来了

  <div class="preview-list" id="canvas-area">
    <component
      v-for="component in components" 
      :key="component.id"
      :is="componentsname[component.name]"
      v-bind="component.props" 
      />
  </div>
<script setup lang="ts">
import ltext from '@/components/ltext'
import { ref, shallowreactive } from 'vue'
interface styleprops = {
   text: string;
   fontsize: string;
}
interface componentdata = {
  id: number;
  name: string;
  props?: styleprops;
}
type componentname = {
  [key: string]: any
}
const components = ref<componentdata[]>([
    { id: 1, name: 'ltext', props: { text: 'hello', fontsize: '12px'}},
    { id: 2, name: 'ltext', props: { text: 'hello2', fontsize: '14px'}},
    { id: 3, name: 'ltext', props: { text: 'hello3', fontsize: '16px'}}
])
// 解决方案
const componentsname = shallowreactive<componentname>({
  ltext
})

</script>

拓展:vue3使用动态组件 component

一、动态组件的概念

多个组件通过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>

到此这篇关于vue3动态组件component不生效问题解决方法的文章就介绍到这了,更多相关vue3 component不生效内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com