第一种:使用Record<string, unknown>
Record<string, unknown>表示一个对象,键是string类型,值是未知的
typescript
import { defineProps, PropType } from 'vue';
const props = defineProps({
dataList: {
type: Array as PropType<Record<string, unknown>[]>,
default: () => [],
}
})
第二种:使用索引签名
索引签名表示任何键都是字符串,值可以是任何类型
typescript
import { defineProps, PropType } from 'vue';
interface obj {
[key: string]: unknown
}
const props = defineProps({
dataList: {
type: Array as PropType<obj[]>,
default: () => [],
}
})
PropType
PropType 是 Vue 提供的工具类型,用于定义复杂的 prop 类型
要使用,先安装
typescript
npm install vue @vue/runtime-core
然后在页面中import
typescript
import { PropType } from 'vue';