【问题标题】:How to show a image in React from a JSON file如何在 React 中显示 JSON 文件中的图像
【发布时间】:2021-10-09 23:38:40
【问题描述】:

// JSON File
{
    "project": [
        {
            "id": "0",
            "title": "Old Portfolio",
            "images" :{
                "main": "portfolio.png",
                "second": ""
            },
            "content": ["This is one of my first few projects I made as a beginner", "At this project, I learned using JavaScript for making interactive elements like a navbar button. This project was not really something I'm proud of. Hence why I'm making a new portfolio but now with react"]
            ,
            "tags": ["HTML5", "CSS", "JavaScript", "jQuery"]
        },
        {
            "id": "1",
            "title": "Twitter Clone",
            "images" :{
                "main": "twitter_clone.png",
                "second": ""
            },
            "content": ["Project I made to improve my skills in PHP and MySQL.", "At this project, I used MySQL to store data and use PHP to alter what my website will show to the user. I've also used AJAX and JQuery for processing data from MySQL with PHP."]
            ,
            "tags": ["HTML5", "CSS", "JavaScript", "PHP", "MySQL", "jQuery", "AJAX"]
        }
    ]
}


// projectList is from a JSON file
// ProjectList.js
{projectList.map((project) => (
    <Projects key={project.id} projectData={project}/>
))}


// Projects.js
const imagePath = "../images/";
const mainImage = imagePath + projectData.images.main

const Projects = ({projectData}) => {
  <img style={{
      width: "100%",
  }} src={mainImage}alt="" />
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
我有一个存储图像的 JSON 文件。这将是一个项目列表。 图像 src 不起作用,我无法在函数内导入图像。 我希望有人可以帮助我是 React 新手

【问题讨论】:

    标签: javascript reactjs json


    【解决方案1】:
    1. JSON 不应该是对象,我猜在这种情况下你可以使用数组。

    2. 你必须使用缩略图而不是使用“img”,因为你的对象(项目)没有“img”键,但有“缩略图”-> const mainImage = imagePath + projectData.thumbnail.main

    // JSON File
    
        [
            {
                "id": "0",
                "title": "Old Portfolio",
                "thumbnail" :{
                    "main": "portfolio.png",
                    "second": ""
                },
                "content": ["This is one of my first few project I made as a beginner", "At this project I learned using JavaScript for making interactive elements like a navbar button. This project was not really something I'm proud of. Hence why I'm making a new portfolion but now with react"]
                ,
                "tags": ["HTML5", "CSS", "JavaScript", "jQuery"]
            },
            {
                "id": "1",
                "title": "Twitter Clone",
                "thumbnail" :{
                    "main": "twitter_clone.png",
                    "second": ""
                },
                "content": ["Project I made to improve my skills in PHP and MySQL.", "At this project I used MySQL to store data and use PHP to alter what my website will show to the user. I've also used AJAX and JQuery for processing data from MySQL with PHP."]
                ,
                "tags": ["HTML5", "CSS", "JavaScript", "PHP", "MySQL", "jQuery", "AJAX"]
            }
        ]
    
    
    
    // projectList is from a JSON file
    // ProjectList.js
    {projectList.map((project) => (
        <Projects key={project.id} projectData={project}/>
    ))}
    
    
    // Projects.js
    const imagePath = "../images/";
    const mainImage = imagePath + projectData.thumbnail.main
    
    const Projects = ({projectData}) => {
      <img style={{
          width: "100%",
      }} src={mainImage}alt="" />
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

    【讨论】:

    • 你能解释一下哪里出了问题,你做了什么改变吗?仅代码的答案更有可能被否决。
    • 1. JSON 不应该是对象,我想在这种情况下你可以使用数组。 2.你必须使用缩略图而不是使用“img”,因为你的对象(项目)没有“img”键,但有“thumbnail”-> const mainImage = imagePath + projectData.thumbnail.main
    • edit提供新信息的答案。
    • 在我的代码中它实际上是图像,我只是忘记在我的帖子中更改它,但问题仍然是一样的。当我将主图像路径放在img src中时,图像不会出现。左上角只会有一个破碎的图像。
    • 当我 console.log 它的路径是正确的。即使我对图像 src 进行了硬编码,仍然没有图像出现
    【解决方案2】:

    我终于找到了办法, 我把图像放在公共文件夹“public/images/image.png” 和

    const Projects = ({projectData}) => {
        <img src={`images/${projectData.images.main}`}/>
    }

    【讨论】:

      猜你喜欢
      • 2021-09-12
      • 1970-01-01
      • 2022-01-21
      • 2021-02-17
      • 1970-01-01
      • 2021-07-31
      • 1970-01-01
      • 1970-01-01
      • 2021-08-05
      相关资源
      最近更新 更多