如何在`< img src =; quord'中添加URL``在不同的卡片中显示图像?

发布于 2025-02-13 21:50:03 字数 1560 浏览 0 评论 0原文

我在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以在不同的卡中显示图像?

image

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?

image

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

难如初 2025-02-20 21:50:03
{test.map(tes=>(<div key={tes.id}>
  <div className="card" style={{ width: "18rem" }}>
    <img src={tes.url} 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>))}

您需要了解地图如何工作代码的问题是您要返回H1,但是您需要返回完整的卡部门,我建议您创建一个卡组件,然后通过道具

{test.map(tes=>(<div key={tes.id}>
  <div className="card" style={{ width: "18rem" }}>
    <img src={tes.url} 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>))}

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

乖乖哒 2025-02-20 21:50:03

您使用地图方法在“ Div” Elems中显示了URL列表。

使用IMG标签而不是DIV进行相同的操作:

{test.map(tes =>
    (<img src={tes.url} className="card-img-top" alt="..." />)
)}

You displayed the url list in 'div' elems using the map method.

Do the same using an img tag instead of a div :

{test.map(tes =>
    (<img src={tes.url} className="card-img-top" alt="..." />)
)}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文