通过Typescript中的对象进行Map

flseospp  于 2023-01-21  发布在  TypeScript
关注(0)|答案(1)|浏览(218)

我试着查找一些指南,但他们没有提供任何实质性的数据。这是我在JSX中的设置。
目的:

export const Project1 =[ 
    {
        name: 'Interior Design Website',
        description: 'Responsive and minimalistic',
        tech:[
            'imgurl.com',
            'img2url.com',
            'img3url.com',
        ]
    },

    {
        name: 'Exterior Design Website',
        description: 'Responsive and minimalistic',
        tech:[
            'imgurl.com',
            'img2url.com',
            'img3url.com',
        ]
    },
    {
        name: 'Furniture Design Website',
        description: 'Responsive and minimalistic',
        tech:[
            'imgurl.com',
            'img2url.com',
            'img3url.com',
        ]
    }
]

组件:

import {Project1} from '@/data'
import ProjectArticle from './ProjectArticle'

<div>
        {Project1.map((project,index)=>(
            <ProjectArticle
                index={index}
                {...project}
            />
        ))}

</div>

小工具:

type Props = {}

export default function ProjectArticle({}: Props) {
  return (
    <div>Work with object data</div>
  )
}

显然这是行不通的,因为这不是Typescript的功能。我真的不喜欢让别人做我的家庭作业,但我绝对卡住了,即使花了2个小时阅读文档。我会感谢即使是最小的指导!
我尝试使用传统的JS逻辑,但它完全不兼容(这是理所当然的)。

wbrvyc0a

wbrvyc0a1#

组件:

import {Project1} from '@/data'
import ProjectArticle from './ProjectArticle'

<div>
  {Project1.map((project, index)=>(
     <ProjectArticle index={index} project={project} />
   ))}
</div>

小工具:

type Project = {
  name: string;
  description: string;
  tech: string[];
}

type Props = {
 project: Project;
}

export default function ProjectArticle({project}: Props) {
  return (
   <div>
    <div>Name: {project.name}</div>
    <div>Description: {project.description}</div>
    <div>Tech:</div>
    <div>{project.tech.map((item: string) => <p key={item}>{item}</p>)}</div>
  </div>
  )
}

相关问题