简体   繁体   English

找不到图像:为什么 React 声称图像在 src 文件夹之外,而实际上它们不在?

[英]Images not found: Why does React claim the images are outside src folder when they are not?

I recently switched to create-react-app as I had issues in production.我最近切换到 create-react-app,因为我在生产中遇到了问题。

Now, I have this folder structure with the images being inside my asset folder which is inside my src folder:现在,我有这个文件夹结构,图像在我的 asset 文件夹中,它在我的 src 文件夹中:

在此处输入图像描述

At the top of my Main.js-file I seek to import my image files like so:在我的 Main.js 文件的顶部,我试图像这样导入我的图像文件:

import Pic from "../assets/Aalto.png"

However, I get this error from devserver: "Module not found: Error: You attempted to import../assets/Aalto.png which falls outside of the project src/ directory. Relative imports outside of src/ are not supported. You can either move it inside src/, or add a symlink to it from project's node_modules/."但是,我从 devserver 收到此错误:“未找到模块:错误:您试图导入../assets/Aalto.png,它位于项目 src/ 目录之外。不支持 src/ 之外的相对导入。您可以要么将它移动到 src/ 中,要么从项目的 node_modules/ 添加一个符号链接到它。”

-> this code worked flawlessly before my switch to create-react-app AND when trying to import "./Website_pic.jpg" instead (which you can see in the picture), which is not inside my asset folder, then the problem does not arise. -> 在我切换到 create-react-app 之前以及尝试导入“./Website_pic.jpg”(你可以在图片中看到)之前,这段代码完美无缺地工作,它不在我的资产文件夹中,那么问题就来了没有出现。

How can I fix this?我怎样才能解决这个问题?

You are trying to go out of the src folder by using ..您正在尝试使用 go 从src文件夹中取出..

Change this改变这个

import Pic from "../assets/Aalto.png"

To this:对此:

import Pic from "./assets/Aalto.png"

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

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