当前位置: 代码网 > it编程>前端脚本>Vue.js > vue3 v-for遍历defineProps或者props接收的数据时,报“xx” is of type ‘unknown‘

vue3 v-for遍历defineProps或者props接收的数据时,报“xx” is of type ‘unknown‘

2024年07月28日 Vue.js 我要评论
vue中使用ts,且在使用props或者defineProps进行父传子时,v-for遍历收到的数组,进行取值时,报“xx” is of type 'unknown'

项目场景:

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
    }
})
(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com