Vue3 defineProps 定义对象数组类型指南
在Vue3中,可以使用defineProps来定义对象数组类型。以下是一个示例:
import { defineComponent, defineProps } from 'vue';
export default defineComponent({
props: {
items: {
type: Array as () => Array<{ id: number, name: string }>,
required: true
}
},
setup(props) {
// 使用props.items
}
});
在上面的示例中,items是一个对象数组类型的prop。使用type选项来定义数组中的对象类型,这里使用了一个箭头函数来指定类型。在这个示例中,items数组中的每个对象都应该有一个id属性和一个name属性。
在setup函数中,你可以像使用普通的props一样使用props.items。
原文地址: https://www.cveoy.top/t/topic/qxWC 著作权归作者所有。请勿转载和采集!