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

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

相关推荐
console.log('npc')41 分钟前
Git 冲突与 AI 协助指南
前端·人工智能·git·大模型
爱学堂IT分享2 小时前
Cesium可视化系统实战课程-Cesium教程学习
前端
糖墨夕2 小时前
理解大语言模型:Agent 的“大脑”
前端·agent
Rain的Java大神之路2 小时前
JavaWeb开发如何解决跨域问题
java·前端·后端·nginx·web安全·面试·运维开发
cpolar技术支持3 小时前
浏览器也能跑本地 AI:用 Transformers.js + WebGPU 做一个最小推理 Demo,cpolar 给同事远程体验
前端·ai·cpolar·webgpu·transformers.js
计算机毕设定制辅导-无忧学长3 小时前
《基于SpringBoot青年公寓出租管理系统的设计与实现》
java·vue.js·spring boot·青年公寓出租管理系统
计算机魔术师4 小时前
OpenAI 发布 GPT-6 Astra:多项基准刷新纪录, cybersecurity 能力达 Critical 阈值
前端
xy34534 小时前
Axure9.0中继器遮罩实现方法
前端·ui·html·axure·原型·产品设计
风骏时光牛马4 小时前
智能任务自动化协同AI工作流
前端
IT_陈寒5 小时前
Python的多线程居然是个假把式?搞清GIL让我少熬三天夜
前端·人工智能·后端