项目场景:
vue中使用ts,且在使用props或者defineprops进行父传子时,v-for遍历收到的数组,进行取值时,报“xx” is of type 'unknown'
问题描述
原因分析:
解决方案一:使用接口进行类型声明
<script setup lang="ts">
interface itable {
date: string,
name: string,
address: string,
phone?: number,
}
interface icolumns {
prop: string,
label: string,
type?: string,
width?: string | number,
}
defineprops<{ columndata: icolumns[], tabledata: itable[] }>()
</script>
解决方案二:用vue3的type proptype
import { defineprops, type proptype } from 'vue'
import type { typecolumn } from './index' // ps:这里引入要写前面type
const props = defineprops({
tabledata: {
type: array,
default: () => [],
require: true
},
columndata: {
type: array as unknown as proptype<[typecolumn]>, // 需要先定义unknown
default: () => []
}
})
解决方案三:把接受的数据设为any类型
const props = defineprops({
columndata:{
type: array<any>,
default:() =>[],
require: true
}
})
发表评论