最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - <img src='img.png'> not showing local file (react) - Stack Overflow

programmeradmin1浏览0评论

I have an img tag in javascript that is not showing the file (local file). The file is in a folder called images and the js file I'm trying to use the image is in a folder called Pages. both folders are in the same directory.

<img src='../images/img.png' alt='template'/>

This is the way im using the tag and the browser is showing the alt name with the icon on an invalid image

I have an img tag in javascript that is not showing the file (local file). The file is in a folder called images and the js file I'm trying to use the image is in a folder called Pages. both folders are in the same directory.

<img src='../images/img.png' alt='template'/>

This is the way im using the tag and the browser is showing the alt name with the icon on an invalid image

Share Improve this question edited Dec 17, 2019 at 18:33 Pedro Fontes asked Dec 17, 2019 at 18:27 Pedro FontesPedro Fontes 1743 silver badges12 bronze badges 2
  • 1 Can you share your full file structure? Perhaps the relative path is incorrect. – dkershaw Commented Dec 17, 2019 at 18:28
  • @dkershaw made an edit with it – Pedro Fontes Commented Dec 17, 2019 at 18:33
Add a ment  | 

3 Answers 3

Reset to default 6

You can use relative path when the image is in the public folder of your app, whatever is better to pass image through webpack, so that way you just need to import the image as a normal object and use it into your ponent.

import template from '../images/image.jpg/

// later ir your code 

<img src={template} alt="image al" />

you need to import the image in react ponent like the following.

import image from '../images/img.png';

and use in the img tag like the following

<img src={image} />

and it will work.

If this is a React project you need to require the image in your src like this:

src={ require('../images/img.png') }
发布评论

评论列表(0)

  1. 暂无评论