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

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

相关推荐
-今昭-13 分钟前
Logstash 管理
java·服务器·前端
名字还没想好☜24 分钟前
React 用自定义 useDebounce Hook 治好搜索框频繁请求:防抖、竞态取消与卸载清理
前端·javascript·react.js·react·hooks
杉氧40 分钟前
Flutter 跨平台多端适配与 Android/iOS 一键自动化打包发布
android·前端·flutter
障碍的枫子41 分钟前
xss和CSRF的攻击和防御
前端·xss·csrf
用户921080262861 小时前
3. 给 Bubble 新增一个 Agent Variant
前端
抓不住时间的沙1 小时前
butterfly主题美化,打造属于自己的个性博客
java·开发语言·前端·javascript·node.js·github
kyriewen1 小时前
前端切图仔被 AI 新闻淹死的第 N 天,我用 TRAE Work 定时任务救了自己
前端·人工智能·trae
京东云开发者1 小时前
【全栈实践】第一个 AI Agent 项目:从零搭建 AI 音频创作助手(高级篇)
前端·agent·音视频开发
AbrahamCS1 小时前
从角色协同到可运行图:Graph Engineer 与多智能体编排
前端·人工智能·智能体
BigTopOne2 小时前
VS Code IntelliSense 配置(解决 #include 找不到的报红)
前端