如何修复从Json到React组件呈现本地路径,
我在互联网上到处寻找答案,但没有找到答案:
NOT WORKING :(
[
{
"name": "Apple",
"id": "001",
"price": "$995",
"imgUrl": "./Pictures/219.jpg"
}
]
WORKING, BUT TURTLE SPEED :/
[
{
"name": "Apple",
"id": "001",
"price": "$995",
"imgUrl": "http://www.placekitten.com/200/200"
}
]从“React”类导入react产品扩展了React.Component {React.Component(Props){ super(props) }
render() {
return (
<div>
<img src={this.props.imgUrl}> />
<p>Name: {this.props.name}</p>
<p>Id: {this.props.id}</p>
<p>Price: {this.props.price}</p>
</div >
)
}}
发布于 2019-08-17 11:07:55
因为我不确定你是不是在用Webpack为你的影像服务。我假设你不是,这里是从你的public目录中提供图像的基本方法。
下面是结构:
public
|-assets
|-pictures
|-219.jpg
|-src
|-myImgs.json
|-App.jsjson文件:
{
"name": "Apple",
"id": "001",
"price": "$995",
"imgUrl": "./pictures/219.jpg"
}App.js
import React from 'react';
import myImgs from './myImgs.json'
class App extends React.Component {
...
render() {
return(
<>
<img src={myImgs.imgUrl} alt={myImgs.name} />
</>
)
};
...
};https://stackoverflow.com/questions/57532908
复制相似问题