[英]Newbie trying a react.js project. When I try to import an image I get an error message that it failed to compile
./src/components/Page/Page.js Module not found: Can't resolve './Page/baby.jpeg' in '/Users/kim/Desktop/untitled folder 2/Development/Academy/Projects/portfolio/src/components/Page'
./src/components/Page/Page.js Module not found: Can't resolve './Page/baby.jpeg' in '/Users/kim/Desktop/untitled folder 2/Development/Academy/Projects/portfolio/src /组件/页面'
import React, { Component } from 'react';
import './Page.css';
import baby from './Page/baby.jpeg';
class LandingPage extends Component {
constructor(props) {
super(props);
this.state = {};
}
render() {
return (
<div name='page' className='Page'>
<h1>Hello,.</h1>
{/* <hr className='break' */}
<p>WELCOME</p>
<img src={baby} width="100" height="50" />
</div>
)
}
}
The problem here is with the image location referencing.这里的问题在于图像位置参考。
If your folder structure is like this如果你的文件夹结构是这样的
Page -> LandingPage.js
Page -> Page.css
Page -> baby.jpeg
Then you should import the image like this然后你应该像这样导入图像
import baby from './baby.jpeg';
This should solve your path reference issue because LandingPage component is in the same folder as that of image.这应该可以解决您的路径引用问题,因为 LandingPage 组件与图像位于同一文件夹中。
Here is a sample codesandbox with these assumptions这是一个带有这些假设的示例代码框
https://codesandbox.io/s/awesome-snowflake-dp8z1?file=/src/App.js https://codesandbox.io/s/awesome-snowflake-dp8z1?file=/src/App.js
I imagine you've gathered this from the comments, but you need to ensure you have the correct path for "baby.jpeg."我想你是从评论中收集到的,但你需要确保你有正确的“baby.jpeg”路径。 It should look in the local directory to find the resource(relative path) unless you specifically give it a global path.
它应该在本地目录中查找资源(相对路径),除非你专门给它一个全局路径。 Since the "baby.jpeg" is supposed to be in "Page" with your code, perhaps you should try using the path "./baby.jpeg" or change to a global path.
由于“baby.jpeg”应该与您的代码一起在“页面”中,也许您应该尝试使用路径“./baby.jpeg”或更改为全局路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.