ionic-framework - 类型上不存在属性 - 无法在组件中获取道具
问题描述
我是反应新手,我不知道如何将道具发送到组件通过 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
解决方案
推荐阅读
- arrays - Removing specific Value from an array using .remove() . But why im not getting the correct answer?
- php - 我可以在哪里放置在我的 laravel 应用程序上生成 pdf 响应的逻辑?
- javascript - Which is the easiest way to call a functional component in button onClick in React js
- java - Hazelcast优雅方式处理com.hazelcast.nio.serialization.HazelcastSerializationException java.io.InvalidClassException
- r - 如何从表达式创建 data.frame?
- angular - 您在预期流的位置提供了“未定义”。HttpInterceptor 中的错误
- python - for 循环在我的 discord.py 机器人中运行得太快
- c# - 从 Collider2D 数组中获取位置
- java - 即使根线程引用了其子对象之一,GC 根线程不再可以访问父对象是否符合 Java GC 的条件?
- c++ - 不同硬件上的不同 OpenGL 行为