简体   繁体   English

在反应中从目标文件加载图像时出现问题

[英]Problem loading images from object file in react

I have an object file and can't seem to get the images to load from that file, if I do an import in the index file and call it, the images load and work fine but if I try it from the object file then it doesn't work.我有一个目标文件,似乎无法从该文件加载图像,如果我在索引文件中进行导入并调用它,图像将加载并正常工作,但是如果我从目标文件中尝试它,那么它不起作用。 I'm not getting any errors in the console and have tried listing the images from different locations.我在控制台中没有收到任何错误,并尝试列出来自不同位置的图像。

Here's the object file这是目标文件

export const projects = [
  {
    id: "0",
    image: "../../images/mayhemds.png",
    alt: "TelecomPlus",
    title: "TelecomPlus",
    desc: "Built the TelecomPlus website on the Apostrophe CMS, using the tech stack listed below.",
    tags: ["HTML", "JavaScript", "SCSS", "Apostrophe CMS"],
    source: "https://google.com",
    visit: "https://goggle.com",
  },
  {
    id: "1",
    image: "/public/data_trend.png",
    alt: "telecomplus",
    title: "Project 2",
    desc: "Lorem ipsum, dolor sit amet consectetur adipisicing elit.",
    tags: ["Html", "JavaScript", "SCSS", "Nunjucks", "Git"],
    source: "https://google.com",
    visit: "https://goggle.com",
  },
  {
    id: "2",
    image: "../../images/telecomplus.png",
    alt: "telecomplus",
    title: "Project 3",
    desc: "Lorem ipsum, dolor sit amet consectetur adipisicing elit.",
    tags: ["Html", "JavaScript", "SCSS", "Nunjucks", "Git"],
    source: "https://google.com",
    visit: "https://goggle.com",
  },
  {
    id: "3",
    image: "../../images/telecomplus.png",
    alt: "telecomplus",
    title: "Project 4",
    desc: "Lorem ipsum, dolor sit amet consectetur adipisicing elit.",
    tags: ["Html", "JavaScript", "SCSS", "Nunjucks", "Git"],
    source: "https://google.com",
    visit: "https://goggle.com",
  },
];

Here is the index.js file这是 index.js 文件

import { projects } from "./projects";
import "./projects.css";

const Projects = () => {
  return (
    <>
      <div className="project-section" id="projects">
        <div className="project-container">
            <h2 className="project-heading">Projects</h2>
          <div className="card-wrap">
            {projects.map(
              ({ id, image, title, desc, tags, source, visit, alt }) => (
                <div className="project-card" key={id}>
                  <img className="project-image" src={image} alt={alt} />
                  <h3 className="title">{title}</h3>
                  <p className="desc">{desc}</p>
                  <div className="stack">
                    <h4 className="stackTitle">Stack</h4>
                    <ul className="tag-list">
                      {tags.map((tag, i) => (
                        <li className="tag-list-item" key={i}>
                          {tag}
                        </li>
                      ))}
                    </ul>

                    <div className="btn-wrap">
                      <div className="btn" href={source}>
                        Code
                      </div>
                      <div className="btn" href={visit}>
                        Website
                      </div>
                    </div>
                  </div>
                </div>
              )
            )}
          </div>
        </div>
      </div>
    </>
  );
};

export default Projects;

I can't seem to see what's missing.我似乎看不到缺少什么。 Any help would be truly grateful!任何帮助将不胜感激!

Unless your files public folder, you need to let webpack include it in the bundle, therefore you should use import / require with its path:除非您的文件是public文件夹,否则您需要让 webpack 将其包含在包中,因此您应该使用import / require及其路径:

// image === "../../images/mayhemds.png"
<img className="project-image" src={require(image)} alt={alt} />

It explained in CRA docs .它在 CRA 文档中进行了解释。

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

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