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

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

相关推荐
软件技术NINI22 分钟前
娃娃店html+css 4页
前端·css·html
VX:Fegn089526 分钟前
计算机毕业设计|基于springboot + vue乡村振兴服务系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
wordbaby29 分钟前
TanStack Router 路径参数(Path Params)速查表
前端
盟接之桥1 小时前
盟接之桥--说制造:从“找缝隙”到“一万米深”——庖丁解牛式的制造业精进之道
大数据·前端·数据库·人工智能·物联网·制造
巴拉巴拉~~1 小时前
Flutter 通用滑块组件 CommonSliderWidget:单值 / 范围 + 刻度 + 标签 + 样式自定义
开发语言·前端·javascript
期待のcode2 小时前
验证码实现
java·vue.js
韭菜炒大葱2 小时前
现代前端开发工程化:Vue3 + Vite 带你从 0 到 1 搭建 Vue3 项目🚀
前端·vue.js·vite
老华带你飞2 小时前
志愿者服务管理|基于springboot 志愿者服务管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·spring
栀秋6662 小时前
面试常考的最长递增子序列(LIS),到底该怎么想、怎么写?
前端·javascript·算法
Melrose2 小时前
Flutter - 使用Jaspr来构建SEO友好网站
前端·flutter