解决vue3按注册名动态渲染组件在setup中无效的问题

在setup语法糖中,按注册名动态渲染组件无效:

javascript 复制代码
<template>
  <component :is="cpnName" />
</template>
<script setup>
  import QuesTypeContent from './QuesTypeContent.vue';
  const cpnName = 'QuesTypeContent';
</script>

解决办法:

  • 1.抛弃setup用选项式api。
  • 2.如果我们必须要使用到组件名渲染时,做如下更改即可生效:
javascript 复制代码
<template>
  <component :is="cpn[cpnName]" />
</template>
<script setup>
  import QuesTypeContent from './QuesTypeContent.vue';
  const cpnName = 'QuesTypeContent';
  const cpn = { QuesTypeContent };
</script>

本质上这还是直接使用组件对象,但解决了特定情况下必须用到组件注册名的问题。

相关推荐
萌萌哒草头将军11 分钟前
绿联云 NAS 安装 AudioDock 详细教程
前端·docker·容器
计算机毕设VX:Fegn089524 分钟前
计算机毕业设计|基于springboot + vue宠物医院管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
GIS之路43 分钟前
GIS 数据转换:使用 GDAL 将 GeoJSON 转换为 Shp 数据
前端
朴shu1 小时前
Luckysheet 远程搜索下拉 控件开发 : 揭秘二开全流程
前端
MediaTea2 小时前
Python:模块 __dict__ 详解
开发语言·前端·数据库·python
字节跳动开源3 小时前
Midscene v1.0 发布 - 视觉驱动,UI 自动化体验跃迁
前端·人工智能·客户端
光影少年3 小时前
三维前端需要会哪些东西
前端·webgl
王林不想说话4 小时前
React自定义Hooks
前端·react.js·typescript
heyCHEEMS4 小时前
Uni-app 性能天坑:为什么 v-if 删不掉 DOM 节点
前端
马致良4 小时前
三年前写的一个代码工具,至今已被 AI Coding 完全取代。
前端·ai编程