【发布时间】:2019-01-10 16:46:07
【问题描述】:
我有一些带有数据的 json,其中一个参数是带有图像的本地文件夹的路径:
// data.json
[
{
"id": 1,
"image": "assets/somepic.png"
},
{
"id": 2,
"image": "assets/anotherpic.png"
}
// similar data goes there
]
然后我将这些数据导入到我的组件中
import data from "data.json"
我想通过这些数据进行映射:
render() {
return (
data.map((item) => (
// Ofc it won't work if I just put {item.image} in src,
// because we need to import image before use it
// that's why I'm trying to use require
<img src={require(item.image)} />
))
)
}
但这行不通。 如何使用图像,在 json 数据中为 create-react-app 指定了哪个本地补丁?
请注意:应用程序不应被弹出。
【问题讨论】:
-
代替地图,试试forEach
-
不,它不起作用
-
您还需要将 img 标签包装在 div 标签中。
-
我为什么需要它?
标签: javascript reactjs create-react-app