Cannot get relative image path in angular 6

坚强是说给别人听的谎言 提交于 2019-12-31 07:47:06

问题


This is the structure of my folders

angular-src
--src
--app
----components
------whatever.html
public
--images
----01.jpg

I want to access 01.jpg from whatever.html.

I tried many alternatives like <img src="../../../../public/images/01.jpg"> or <img src="../../../../../public/images/01.jpg"> and I keep getting 404.

I use angular 6 and node 8.11.1. What am I missing here?

Thanks


回答1:


At first you should move your assets like the img you use into the assets folder which is a direct child of src.

You should then be able to call the image directly by <img src="assets/images/01.jpg">




回答2:


In your .angular.json you can define the glob as this one

"assets": [
    {"glob": "**/*", "input": "../../public", "output": "./assets/"}
]

This will copy the contents of your public folder to the /dist/assets/ directory. Then, use it like

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


来源:https://stackoverflow.com/questions/52274691/cannot-get-relative-image-path-in-angular-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!