解决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>

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

相关推荐
excel1 小时前
HLS TS 文件损坏的元凶:Git 提交与拉取
前端
Aphasia3111 小时前
https连接传输流程
前端·面试
徐小夕1 小时前
万字长文!千万级文档 RAG 知识库系统落地实践
前端·算法·github
梦梦代码精1 小时前
2026年PHP开源商城系统实测对比:架构、多商户、商用授权,谁才是真·省心?
vue.js·docker·架构·开源·代码规范
threelab1 小时前
Three.js 物理模拟着色器 | 三维可视化 / AI 提示词
开发语言·前端·javascript·人工智能·3d·着色器
kyriewen2 小时前
CSS Container Queries:彻底告别 @media 写到手软,附 5 个真实布局案例
前端·css·面试
小小小小宇3 小时前
OpenMemory MCP
前端
和平宇宙4 小时前
AI笔记005. hermes-DeepSeek V4 Pro, 128K上下文引发的探索
前端·人工智能·笔记
IT_陈寒4 小时前
Redis持久化这个坑,我爬了一整天才出来
前端·人工智能·后端
naildingding4 小时前
3-ts接口 Interface
前端·typescript