简体   繁体   English

ReactJS 和公共文件夹中的图像

[英]ReactJS and images in public folder

Im new in ReactJS and I want to import images in a component.我是 ReactJS 的新手,我想在组件中导入图像。 These images are inside of the public folder and I do not know how to access the folder from the react component.这些图像位于公用文件夹中,我不知道如何从反应组件访问该文件夹。

Any ideas?有任何想法吗?

EDIT编辑

I want to import an image inside Bottom.js or Header.js我想在 Bottom.js 或 Header.js 中导入图像

The structure folder is:结构文件夹为:

在此处输入图像描述

I do not use webpack.我不使用 webpack。 Should I?我是不是该?

Edit 2编辑 2

I want to use webpack for loading the images and the rest of assets.我想使用 webpack 来加载图像和资产的 rest。 So in my config folder I have the next files:所以在我的配置文件夹中,我有下一个文件:

在此处输入图像描述

Where I need to add the paths of the images and how?我需要在哪里添加图像的路径以及如何添加?

Thanks谢谢

You don't need any webpack configuration for this..你不需要任何 webpack 配置。

In your component just give image path.在您的组件中,只需提供图像路径。 By default react will know its in public directory.默认情况下,react 会知道它在公共目录中。

<img src="/image.jpg" alt="image" />

To reference images in public there are two ways I know how to do it straight forward.要在公共场合引用图像,我知道有两种方法可以直接进行。 One is like above from Homam Bahrani.一个就像上面来自 Homam Bahrani 的一样。

using使用

    <img src={process.env.PUBLIC_URL + '/yourPathHere.jpg'} /> 

And since this works you really don't need anything else but, this also works...而且由于这有效,您真的不需要任何其他东西,但是,这也有效......

    <img src={window.location.origin + '/yourPathHere.jpg'} />

the react docs explain this nicely in the documentation, you have to use process.env.PUBLIC_URL with images placed in the public folder.反应文档在文档中很好地解释了这一点,您必须使用process.env.PUBLIC_URL并将图像放置在公共文件夹中。 See here for more info请参阅此处了解更多信息

return <img src={process.env.PUBLIC_URL + '/img/logo.png'} />;

1- It's good if you use webpack for configurations but you can simply use image path and react will find out that that it's in public directory. 1-如果你使用 webpack 进行配置很好,但你可以简单地使用图像路径,react 会发现它在公共目录中。

<img src="/image.jpg">

2- If you want to use webpack which is a standard practice in React. 2- 如果你想使用 webpack,这是 React 的标准做法。 You can use these rules in your webpack.config.dev.js file.您可以在 webpack.config.dev.js 文件中使用这些规则。

module: {
  rules: [
    {
      test: /\.(jpe?g|gif|png|svg)$/i,
      use: [
        {
          loader: 'url-loader',
          options: {
            limit: 10000
          }
        }
      ]
    }
  ],
},

then you can import image file in react components and use it.然后你可以在反应组件中导入图像文件并使用它。

import image from '../../public/images/logofooter.png'

<img src={image}/>

在公共 ex.Assets 中创建一个文件夹并将您的图像放在该文件夹中并在 src 中分配 folder_name / image_name

<img src = "/Assets/cardimg.svg" alt="Card image cap" width="400" />

The react components in the components folder have no notion of the public directory. components文件夹中的react组件没有公共目录的概念。 As a result something like './images...' will not work. 结果像'./images...'这样的东西不起作用。 Rather in your button component have an import like this: 而在你的按钮组件中有这样的导入:

import myImage from '../../public/images/logofooter.png'

This is the preferred way of importing static resources into a react component. 这是将静态资源导入反应组件的首选方法。 And you can make use of this import like so: 你可以像这样使用这个导入:

<Button srcAsProp={myImage}/>

or simply: 或者干脆:

<div><img src={myImage}/></div>

We know React is SPA.我们知道 React 是 SPA。 Everything is rendered from the root component by expanding to appropriate HTML from JSX.通过从 JSX 扩展为适当的 HTML,所有内容都从根组件呈现。

So it does not matter where you want to use the images.因此,您想在哪里使用图像并不重要。 Best practice is to use an absolute path (with reference to public).最佳做法是使用绝对路径(参考公共)。 Do not worry about relative paths.不要担心相对路径。

In your case, this should work everywhere:在您的情况下,这应该适用于任何地方:

"./images/logofooter.png"

Simply Use只需使用

<img src='/image.extension' />

React will automatically point toward the public directory React 会自动指向公共目录

You should use webpack here to make your life easier.你应该在这里使用 webpack 来让你的生活更轻松。 Add below rule in your config:在您的配置中添加以下规则:

const srcPath = path.join(__dirname, '..', 'publicfolder')

const rules = []

const includePaths = [
  srcPath
]
    // handle images
    rules.push({
      test: /\.(png|gif|jpe?g|svg|ico)$/,
      include: includePaths,
      use: [{
        loader: 'file-loader',
        options: {
          name: 'images/[name]-[hash].[ext]'
        }
      }

After this, you can simply import the images into your react components:在此之后,您可以简单地将图像导入到您的反应组件中:

import myImage from 'publicfolder/images/Image1.png'

Use myImage like below:使用 myImage 如下:

<div><img src={myImage}/></div>

or if the image is imported into local state of component或者如果图像被导入到组件的本地状态

<div><img src={this.state.myImage}/></div> 

here is sure and three simple way to do that...这是肯定的和三种简单的方法来做到这一点......

  1. you can make one folder in directory and access it as we do import way or您可以在目录中创建一个文件夹并像我们导入方式一样访问它或

  2. you can give direct image name in src您可以在src中直接给出图像名称

    <img src="image__name" alt="yourpic" />

//by default react look in public folder can render image in img tag //默认情况下react look in public folder可以在img标签中渲染图片

  1. const image = window.location.origin + "/image.png";

// if your image in public directory inside folder imagecollection than you can import it in this way // 如果您的图像在文件夹 imagecollection 内的公共目录中,那么您可以通过这种方式导入它

  const image = window.location.origin + "/imagecollection /image.png";
 <img src={image} alt="yourpic" />

Try This One its easy and Simple试试这个简单易行

  1. Don't Make Image folder in src.不要在 src 中创建 Image 文件夹。
  2. Make an image folder in public.公开制作一个图像文件夹。
  3. you work in react-bootstrap install你在 react-bootstrap install 中工作

npm install react-bootstrap npm install react-bootstrap

 import React from 'react'; import 'bootstrap/dist/css/bootstrap.min.css'; import { Image } from 'react-bootstrap'; export default function Main() { return ( <> <Image src="/img/image.jpg/100px250" alt="bg image" fluid /> </> ) }

Hope it's Done希望它完成了

A simple solution is to use paths like this /images/logoFooter.png .一个简单的解决方案是使用这样的路径/images/logoFooter.png If the file is located directly under the public folder, do /someImage.png .如果文件直接位于public下,请执行/someImage.png You can go deeper, /x/y/z/image.png .您可以更深入地/x/y/z/image.png Treat the locating part of the image as an absolute kind of location for that image.将图像的定位部分视为该图像的absolute位置。

For more information, check out https://create-react-app.dev/docs/using-the-public-folder/ .有关更多信息,请查看https://create-react-app.dev/docs/using-the-public-folder/

if you want to add your javascript file from public folder to react, put specific file to index.html file in public folder.如果您想从公用文件夹添加您的 javascript 文件以做出反应,请将特定文件放入公用文件夹中的 index.html 文件。 Your problem will be solve.你的问题将得到解决。

你也可以使用这个..假设'yourimage.jpg'在你的公共文件夹中。

<img src={'./yourimage.jpg'}/>

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

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