【问题标题】:using Array map method in TypeScript在 TypeScript 中使用数组映射方法
【发布时间】:2020-06-15 13:13:57
【问题描述】:

如何使用诸如带有 Typescript 的 array.map 方法将 JSON 数据动态呈现到组件中。例如。我从下面的代码中得到一个错误。

const PricingSection: FC<IProps> = ({ icon, title, price, user, observations,
projects, interviews, buttonType }) => 

const mapped = map(datas => (

        <ul style={{ display: 'flex' }}>
            {datas.icon}
        </ul>
    )

【问题讨论】:

  • 你尝试过什么,它到底有什么问题?
  • 我试过使用这样的东西:从'./data.json'导入数据 const mapped = data.map(datas => {
    它不工作
  • 您遇到了什么错误?
  • (parameter) datas: any Parameter 'datas' 隐含一个 'any' type.ts(7006)

标签: arrays reactjs typescript react-tsx


【解决方案1】:

您似乎离我们不远了...假设您要遍历的数组是“观察”道具:

const mapped = observations.map(datas => (

        <ul style={{ display: 'flex' }}>
            {datas.icon}
        </ul>
    )

响应您调用 (parameter) datas: any Parameter 'datas' implicitly has an 'any' type.ts(7006) 的评论链,这更像是一个 linter 输入标注而不是错误...... 这是说你隐含地拥有类型

(datas: any => 

您可以通过在 datas 对象上显式放置一个类型来解决它,如下所示:

(datas: DataObject =>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-18
    • 1970-01-01
    • 2018-07-26
    • 2018-10-26
    • 2014-03-11
    • 2019-02-16
    • 2021-10-10
    相关资源
    最近更新 更多