首页 > 解决方案 > 类型上不存在属性 - 无法在组件中获取道具

问题描述

我是反应新手,我不知道如何将道具发送到组件通过 axios get call 从 wordpress 站点获取帖子并映射响应后,我无法通过“点”访问元素,post.id但只能作为数组post['id']

{posts.map(post => (
       // console.log(post.id)
       // the log return Property 'id' does not exist on type 'never' 
       <PostItem key={post['id']} post={post}/>
    ))}

在 PostItem 组件中,我得到了道具 post 和 key 但我不能像 post.id 或 post.title 这样的帖子数据

知道如何正确使用道具吗?

谢谢

这是我的 PostItem 组件的完整代码

import React, { Component } from 'react'
// import axios from 'axios'
type Props = {
    post: any[];
    key: string;
}

export class PostItem extends React.Component<Props>{

    render() {
        //  const {post} = this.props.post; // return : Property 'post' does not exist on type 'any[]'
        const post = this.props.post;
        console.log(post); //return the post data
        console.log(post.id); // return Property 'id' does not exist on type 'any[]'
        //console.log(post['id']); // return Element implicitly has an 'any' type because index expression is not of type 'number'.  TS7015

        return (
           <div></div>

        )
    }
}

export default PostItem

标签: ionic-frameworkaxiosionic-react

解决方案


推荐阅读