【发布时间】:2019-07-15 03:33:30
【问题描述】:
首先我要明确这一点,我使用 cdn 进行反应,并使用 ajax 从 json 文件中获取详细信息。
所以,我有一个 json 文件 reactjs.json,看起来像...
[
{
"e64fl7exv74vi4e99244cec26f4de1f":[ "image_1.jpg","image_2.jpg"]
}
]
index.html
<!DOCTYPE html>
<html>
<head>
<title>Image Viewer-Static</title>
</head>
<body>
<div id="root"></div>
<script src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/babel-standalone@6.15.0/babel.min.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script type="text/babel">
class FetchDemo extends React.Component {
constructor(props) {
super(props);
this.state = {
images: []
};
}
componentDidMount() {
axios.get('reactjs.json').then(res => {
console.log(res.data);
this.setState({ images: res.data });
});
}
render() {
const { images } = this.state;
return (
<div>
{this.state.images.map((images, index) => (
<PicturesList key={index} apikeys={images.e64fl7exv74vi4e99244cec26f4de1f} />
))}
</div>
);
}
}
class PicturesList extends React.Component {
render() {
return (
<img src={this.props.apikeys} alt={this.props.apikeys}/>
);
}
}
ReactDOM.render(
<FetchDemo/>,
document.getElementById("root")
);
</script>
</body>
</html>
我想显示名为 image_1.jpg,image_2.jpg 的图像,但 this.props.apikeys 获取的值类似于 image_1.jpg,image_2.jpg
但我希望它给出两个值并显示两个图像。
我尝试了很多来解决这个问题,但失败了。欢迎任何建议和帮助。
【问题讨论】:
-
您能否提供给我们,您如何从您的链接访问图像? src 期望公共 URL 有效,例如
http://www.example.com/image.png
标签: reactjs