【发布时间】:2021-09-06 21:23:37
【问题描述】:
我有这个由 BE 生成的 Json 对象:
{
"step1": {
"approved": true,
"approvalTime": "10-11-2021",
"title": "title 1",
"description": "description 1"
},
"step2": {
"approved": true,
"approvalTime": "10-11-2021",
"title": "title 2",
"description": "description 2"
},
"step3": {
"approved": true,
"approvalTime": "10-11-2021",
"title": "title 3",
"description": "description 3"
}
}
材料时间线:
<Timeline align="alternate">
<TimelineItem>
<TimelineOppositeContent>
<Typography variant="body2" color="textSecondary"> // get color form Json
9:30 am // get this value from JSON
</Typography>
</TimelineOppositeContent>
<TimelineSeparator>
<TimelineDot>
<LibraryBooks />
</TimelineDot>
<TimelineConnector />
</TimelineSeparator>
<TimelineContent>
<Paper elevation={3} className={classesTimeline.paper}>
<Typography variant="h6" component="h1">
title 1 // get title from JSON
</Typography>
<Typography> // get description from JSON</Typography>
</Paper>
</TimelineContent>
</TimelineItem>
<TimelineItem>
<TimelineOppositeContent>
<Typography variant="body2" color="textSecondary">
11:00 am // get this value from JSON
</Typography>
</TimelineOppositeContent>
<TimelineSeparator>
<TimelineDot color="primary">
<PeopleAlt />
</TimelineDot>
<TimelineConnector />
</TimelineSeparator>
<TimelineContent>
<Paper elevation={3} className={classesTimeline.paper}>
<Typography variant="h6" component="h1">
title 2 // get title from JSON
</Typography>
<Typography>description 2 // get value from JSON</Typography>
</Paper>
</TimelineContent>
</TimelineItem>
<TimelineItem>
<TimelineOppositeContent>
<Typography variant="body2" color="textSecondary">
10:00 am // get value from JSON
</Typography>
</TimelineOppositeContent>
<TimelineSeparator>
<TimelineDot color="primary">
<BusinessCenter />
</TimelineDot>
<TimelineConnector />
</TimelineSeparator>
<TimelineContent>
<Paper elevation={3} className={classesTimeline.paper}>
<Typography variant="h6" component="h1">
title 3 // get values from JSON
</Typography>
<Typography>description 3 // get value from JSON</Typography>
</Paper>
</TimelineContent>
</TimelineItem>
</Timeline>
是否可以使用 Typescript 设置来自 JSON 对象的所有值并根据 JSON 值设置 Paper 组件颜色?当我有"approved": true 为Paper 背景设置绿色时。
【问题讨论】:
-
不确定我是否完全关闭。但是这个材质时间轴代码是在 .tsx 文件中吗?当您在变量中拥有结构时(比如说 myjson),您可以简单地在内部引用它。例如。类似
${myjson.approved ? 'green' : 'textSecondary'} -
嗨,Peter,我在您的 Material Timeline 中看到几行引用了 JSON 中未出现的值(例如
color、9:30 am)。我应该从哪里获取这些值? -
你可以暂时忽略它们。
-
我不确定“由 BE 生成”是什么意思。后端? JSON 是来自项目目录中的
.json文件,还是您从 API 获取的内容?你得到的答案很好,但我们都错过了如何让 JSON 作为道具传递。 -
您所说的“来自可靠来源的答案”是什么意思?
标签: javascript reactjs typescript material-ui react-typescript