繁体   English   中英

如何在 React 中显示来自 JSON 文件的图像

[英]How to show a image in React from a JSON file

 // 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 不工作,我无法在 function 中导入图像。我希望有人能帮助我是 React 的新手

  1. JSON 不应该是 object,我想在这种情况下你可以使用数组。

  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>

我终于找到了一种方法,我将图像放在公共文件夹“public/images/image.png”中,然后

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM