如何在`< img src =; quord'中添加URL``在不同的卡片中显示图像?
我在next.js
中使用Bootstrap卡;我观看了一个有关在next.js
中获取API的教程,我的代码看起来像这样:
export const getStaticProps=async ()=>{
const res= await fetch('https://jsonplaceholder.typicode.com/photos');
const data = await res.json();
return {
props:{test:data}
}
}
function home({test}){
return (
<div>
<ul className="nav nav-tabs">
<li className="nav-item">
<a className="nav-link " aria-current="page" href="#">
Active
</a>
</li>
</ul>
{test.map(tes=>(<div key={tes.id}><h1>{tes.url}</h1></div>))}
<div className="card" style={{ width: "18rem" }}>
<img src="..." className="card-img-top" alt="..." />
<div className="card-body">
<h5 className="card-title">Card title</h5>
<p className="card-text">
Some quick example text to build on the card title and make up the bulk of
the card's content.
</p>
<a href="#" className="btn btn-primary">
Go somewhere
</a>
</div>
</div>
</div>
)
}
export default home;
此代码{test.map(tes =&gt;(&lt;(&lt; div) key = {tes.id}&gt;&lt; h1&gt; {tes.url}&lt;/h1&gt;&lt;&lt;/div&gt;))}}
在卡外显示了url;如何在&lt; img src =“ ...”
中添加URL以在不同的卡中显示图像?
I'm using bootstrap cards in Next.js
; I watched a tutorial about fetching API in Next.js
and my code looks like this:
export const getStaticProps=async ()=>{
const res= await fetch('https://jsonplaceholder.typicode.com/photos');
const data = await res.json();
return {
props:{test:data}
}
}
function home({test}){
return (
<div>
<ul className="nav nav-tabs">
<li className="nav-item">
<a className="nav-link " aria-current="page" href="#">
Active
</a>
</li>
</ul>
{test.map(tes=>(<div key={tes.id}><h1>{tes.url}</h1></div>))}
<div className="card" style={{ width: "18rem" }}>
<img src="..." className="card-img-top" alt="..." />
<div className="card-body">
<h5 className="card-title">Card title</h5>
<p className="card-text">
Some quick example text to build on the card title and make up the bulk of
the card's content.
</p>
<a href="#" className="btn btn-primary">
Go somewhere
</a>
</div>
</div>
</div>
)
}
export default home;
This code {test.map(tes=>(<div key={tes.id}><h1>{tes.url}</h1></div>))}
displays the urls outside of the card; how can I add the urls in <img src="..."
to display images in different cards?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要了解地图如何工作代码的问题是您要返回H1,但是您需要返回完整的卡部门,我建议您创建一个卡组件,然后通过道具
You need to understand how map works The issue with your code is you are returning h1 but instead you need to return complete card div i suggest you to create a card component then pass props
您使用地图方法在“ Div” Elems中显示了URL列表。
使用IMG标签而不是DIV进行相同的操作:
You displayed the url list in 'div' elems using the map method.
Do the same using an img tag instead of a div :