简体   繁体   English

新手尝试 react.js 项目。 当我尝试导入图像时,我收到一条无法编译的错误消息

[英]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.

相关问题 为什么当我尝试在 react.js 中获取图像的高度和宽度时它返回未定义 - Why does it return undefined when I try to get the height and width of an image in react.js Webpack无法编译React.js项目 - Webpack failed to compile React.js project 当我尝试将 HTML 文件导入 React 时,出现错误 - When I try to import an HTML file into React, I get an error 如何将uploadcare导入react.js项目? - How can I import uploadcare into a react.js project? 我如何在 react.js 中搜索失败时返回错误消息 - How would I return an error message upon a failed search in react.js 当我尝试在我的 react.js 组件中迭代对象数组时,为什么会出现错误? - Why does error appear when I try to iterate array of objects in my react.js component? 我正在尝试通过单击使用 React.js 的按钮来更改图像,但出现错误 - I am trying to change image by clicking buttons using React.js but getting error 反应.js。 当我尝试使用 FileReader 上传超过 3 个图像文件时,仅上传了 3 个 - React.js . when i am trying to upload more then 3 image files, using FileReader, only 3 uploaded 为什么在React.js + GraphQL中出现此错误:模块构建失败:SyntaxError:意外令牌(10:6) - Why do I get this error in React.js + GraphQL: Module build failed: SyntaxError: Unexpected token (10:6) 尝试了解将babel用于react.js时收到的警告消息 - Trying to understand warning messages I get when using babel for react.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM