第一種:使用Record<string, unknown>
Record<string, unknown>表示一個對象,鍵是string類型,值是未知的
import { defineProps, PropType } from 'vue';const props = defineProps({dataList: {type: Array as PropType<Record<string, unknown>[]>,default: () => [],}
})
第二種:使用索引簽名
索引簽名表示任何鍵都是字符串,值可以是任何類型
import { defineProps, PropType } from 'vue';interface obj {[key: string]: unknown
}
const props = defineProps({dataList: {type: Array as PropType<obj[]>,default: () => [],}
})
PropType
PropType 是 Vue 提供的工具類型,用于定義復雜的 prop 類型
要使用,先安裝
npm install vue @vue/runtime-core
然后在頁面中import
import { PropType } from 'vue';