【发布时间】:2022-06-10 23:02:17
【问题描述】:
我有一个从 API 收集数据的组件。从 API 返回的数据是一个包含详细信息的数组。数组中的一个值是应该呈现的组件的类型,所有其他数据都传递给组件。 我正在尝试根据从数据库返回的值呈现正确的组件,但遗憾的是它不起作用。 我是 Vue 新手,但它在 vue2 中工作,但希望它使用组合 API 在 Vue 3 中工作。
这是我要替换的组件代码:
<component :is="question.type" :propdata="question" />
在浏览器中查看时,这是实际显示的内容,但不使用 SelectInput 组件:
<selectinput :propdata="question"></selectinput>
SelectInput 是我的目录的一个组件,如果我硬编码 :is 值,它会按预期工作,如下所示:
<component :is="SelectInput" propdata="question" />
我的完整组件调用component 组件并交换组件:
<template>
<div class="item-group section-wrap">
<div v-bind:key="question.guid" class='component-wrap'>
<div class="component-container">
<!-- working -->
<component :is="SelectInput" :propData="question" />
<!-- not working -->
<component v-bind:is="question.type" :propData="question" />
</div>
</div>
</div>
</template>
<script setup>
import { defineProps, toRefs } from 'vue';
import SelectInput from "./SelectInput";
import TextareaInput from "./TextareaInput";
const props = defineProps({
question: Object,
});
const { question } = toRefs(props);
</script>
【问题讨论】:
-
question.type的值是多少? -
用
{{ question.type }}检查question.type的值。
标签: vue.js vuejs3 vue-composition-api