javascript 我无法使用React Js连接Json文件中的图像

yqhsw0fo  于 2023-01-19  发布在  Java
关注(0)|答案(1)|浏览(99)

我是新的ReactJs,我不能解决问题,以连接图像路径与React从单独的Json文件,它只显示图像icon
我不明白是图像路径或css代码(*!但我没有为图像编写css * ^^)或其他问题?
源代码
组件--“文件夹名称”
postCard.js
数据--“文件夹名称”
datastore.json
img_reactjs01 --“文件夹名称”
Katana.png
App.js
∮我的密码∮

  • 一米八分一秒 *
import React, { Component } from "react";
import qData from "../data/datastore.json";

class Quotes extends Component{
    render(){
        return(
            <div className="card-stl" onmouseover="this.bgColor='white'">
                {
                    qData && qData.map((s)=>{
                        return(
                            <div className="post_Card" key={s.id}>
                                <picture className="card__picture">
                                    <img src={s.image} alt={s.name} />
                                </picture>
                                <h2>{s.name}</h2>
                                <p>{s.price}</p>
                                <p>{s.description}</p>
                            </div>
                        )
                    })
                }
            </div>
        )
    }
}

export default Quotes;
  • 一米九十一 *
[
    {
        "id": 1,
        "name": "Product 1",
        "image": "../img_reactjs01/Katana.png",
        "price": "$20",
        "description": "This is a description of Product 1"
    },
    {
        "id": 2,
        "name": "Product 2",
        "image": "https://pin.it/5i1L9ZG",
        "price": "$30",
        "description": "This is a description of Product 2"
    },
    {
        "id": 3,
        "name": "Product 3",
        "image": "https://pin.it/2pfRfgY",
        "price": "$40",
        "description": "This is a description of Product 3"
    }
]
  • 一米十寸 *
import Quotes from "./components/postCard";

function App() {
  return (
    <div className="App">
      <Quotes />
    </div>
  );
}

export default App;

对不起我的英语

osh3o9ms

osh3o9ms1#

这是因为你的图片链接不是指向图片的链接,而是指向包含图片的页面的链接,尝试插入以下内容作为第一个json元素,它应该可以工作:

{
    "id": 1,
    "name": "Product 1",
    "image": "https://upload.wikimedia.org/wikipedia/commons/thumb/b/b6/Image_created_with_a_mobile_phone.png/1200px-Image_created_with_a_mobile_phone.png",
    "price": "$20",
    "description": "This is a description of Product 1"
}

相关问题