Path to images not working in Angular 2

Everything that I put on my site works correctly, but every image that I try to put on the site (except for the background) returns a 404 Not Found Error .

All my images are in "Project / src / assets / images", and I tried the following img tags in my html documents. Documents are located in "Project / src / app / tabs".

 <img src="src/assets/images/image_name.jpg"> <img src="/src/assets/images/image_name.jpg"> <img src="./src/assets/images/image_name.jpg"> 

I set the background image for my page using css and it works. This is the selector and attribute that I applied:

 background: url('assets/images/greybackground.jpg') no-repeat center center fixed; 

How to add images to my HTML documents without 404 error?

+5
source share
1 answer

Your html files are located in "Project / src / app / tabs" and you are trying to access from this folder, so the images do not load. Therefore, you need to return from the tab folder and the application folder to get to the base directory for both your code and images. Use ../ to return from the folder. Therefore, after reaching the base directory, simply use regular directory lists.

So the answer

 <img src="../../assets/images/image_name.jpg"> <img src="../../assets/images/image_name.jpg"> <img src="../../assets/images/image_name.jpg"> 
+7
source

Source: https://habr.com/ru/post/1264433/


All Articles