当前位置: 首页 > wzjs >正文

瑞安市住房和城乡建设局网站北京百度快照推广公司

瑞安市住房和城乡建设局网站,北京百度快照推广公司,网站建设大德通,网页制作初体验教案问题: vue3循环渲染动态组件component不生效,页面空白 在vue3使用component动态组件展示组件时,组件就是不展示显示空白。在vue2中使用动态变量component展示组件都是没问题。试了很多方法 踩了很多坑,所以记录下: &l…

问题: vue3循环渲染动态组件component不生效,页面空白

在vue3使用component动态组件展示组件时,组件就是不展示显示空白。在vue2中使用动态变量component展示组件都是没问题。试了很多方法 踩了很多坑,所以记录下:

  <div class="preview-list" id="canvas-area"><componentv-for="component in components" :key="component.id":is="component.name"v-bind="component.props" /></div>
<script setup lang="ts">
import LText from '@/components/LText'
import { ref } from 'vue'
interface styleProps = {text: string;fontSize: string;
}
interface componentData = {id: number;name: string;props?: styleProps;
}
const components = ref<componentData[]>([{ id: 1, name: 'LText', props: { text: 'hello', fontSize: '12px'}},{ id: 2, name: 'LText', props: { text: 'hello2', fontSize: '14px'}},{ id: 3, name: 'LText', props: { text: 'hello3', fontSize: '16px'}}
])
</script>

因为vue3使用的是setup语法,组件只要import导入就行 不需要再像vue2中在components挂载,这样导致我想渲染的组件是没有渲染出来页面出现空白,尝试了很多办法对应的组件里面添加多个script指定对应的组件名,还是没生效

解决方法

使用shallowReactive或者shallowRef把对应的组件名称重新定义下,遍历component时,is采用对象key获取对应的对应的组件,这样组件就显示出来了

  <div class="preview-list" id="canvas-area"><componentv-for="component in components" :key="component.id":is="componentsName[component.name]"v-bind="component.props" /></div>
<script setup lang="ts">
import LText from '@/components/LText'
import { ref, shallowReactive } from 'vue'
interface styleProps = {text: string;fontSize: string;
}
interface componentData = {id: number;name: string;props?: styleProps;
}
type componentName = {[key: string]: any
}
const components = ref<componentData[]>([{ id: 1, name: 'LText', props: { text: 'hello', fontSize: '12px'}},{ id: 2, name: 'LText', props: { text: 'hello2', fontSize: '14px'}},{ id: 3, name: 'LText', props: { text: 'hello3', fontSize: '16px'}}
])
// 解决方案
const componentsName = shallowReactive<componentName>({LText
})</script>
http://www.dtcms.com/wzjs/513863.html

相关文章:

  • 重庆网站页面优化网站统计数据分析
  • 电子商务网站规划的内容页面seo优化
  • 关掉自己做的网站站群seo技巧
  • 凡科网免费做网站郑州网站推广哪家专业
  • 网站建设的目的模板百度电话号码查询
  • 银川做网站推广云服务器免费
  • 东莞网站推广外包写软文能赚钱吗
  • 天津网站建设平台识图找图
  • 广州平台网站建设百度seo网站在线诊断
  • 沈阳模板建站哪家好百度搜索排名优化
  • 装修网站怎么做的好处网站设计制作
  • 昆明做网站外包百度收录权重
  • 用html5做京东网站代码进入百度官网
  • sousou提交网站入口网上国网app推广
  • 微信扫一扫登录网站如何做如何建立个人网址
  • win2012做网站北京网站优化快速排名
  • 和平天津网站建设百度经验悬赏令
  • 刚做的网站在百度上搜不到整站seo优化公司
  • 信誉好的商城网站建设查网站
  • wordpress自媒体主题破解南昌搜索引擎优化
  • 做考研政治真题的网站百度推广登录平台
  • 浙江艮威水利建设有限公司网站快速网站排名提升
  • 南昌网站推广公司网络广告推广方案
  • 小说网站如何做书源滨州seo排名
  • 做外贸去哪个网站找客户百度号码认证平台官网
  • 用织梦建手机网站免费私人网站建设软件
  • 苍南网站建设公司百色seo快速排名
  • 有教做鱼骨图的网站吗关于友情链接说法正确的是
  • php网站设计流程人民日报客户端
  • 外贸网站怎么换域名国内新闻最新5条