How to get to the public directory
Solution 1
The Angular-cli
places all files in the /dist
directory.
To display the image in index.html:
<img src="book4u-logo.svg" />
If you want to use your image in your app you should use a folder in the /src
directory like src/shared/assets/img/book4u-logo.svg
The public folder is for public assets and the content is copied to the /dist
folder.
However, when running ng build --prod
the javascript in the/public
folder is not copied over to /dist
.
There is an open issue on Github:Static JS assets not copying
The image placed in the public folder should be accessible from the dist
folder.
Solution 2
Latest version of angular-cli(angular-cli: 1.0.0-beta.19-3
) provides an assets folder under src directory. You just need to add src property of an image tag as below,
<img src="./assets/logo.png">
Note: When you build the project using ng serve
it will copy your assets
folder to dist
folder.
Solution 3
I solved a similar problem in Angular 4 (I had a css file that was in public folder and I put it in assets folder). The answer would be:
- file path: ./src/assets/book4u-logo.svg
<img src="./assets/book4u-logo.svg>
Related videos on Youtube
Nirgn
BY DAY: Full Stack Software Engineer @ webintPro BY NIGHT: A Ninja Favorite quote: "We choose to go to the moon. We choose to go to the moon in this decade and do the other things, not because they are easy, but because they are hard, because that goal will serve to organize and measure the best of our energies and skills, because that challenge is one that we are willing to accept, one we are unwilling to postpone, and one which we intend to win, and the others, too." - John F. Kennedy
Updated on September 14, 2022Comments
-
Nirgn over 1 year
I use angular-cli in my project, and I want to use a global image - my website logo image.
As I understand the
public
directory at the root of the project (whichangular-cli
created) is for public assets - which seems like a good place to put my image (please correct me if I'm wrong).But, when I try to get to that image in my
<img src="/public/book4u-logo.svg" />
tag, I get 404. The reason is, of course, my project root is thesrc
directory and not the project root directory.So, how should I get to that directory? Or should I place my image in another directory?
My project tree (i remove the unnecessary suff):
. ├── angular-cli-build.js ├── angular-cli.json ├── package.json ├── public │ ├── book4u-logo.svg │ └── hero-image-default.jpg ├── src │ ├── app │ │ ├── bfy.component.html │ │ ├── bfy.component.scss │ │ ├── bfy.component.ts │ │ ├── environment.ts │ ├── favicon.ico │ ├── index.html │ ├── main.ts │ ├── system-config.ts │ ├── tsconfig.json │ └── typings.d.ts ├── tslint.json └── typings.json
The
img
tag is at/src/app/bfy.component.html
and the image itself is at/public/book4u-logo.svg
. -
72GM about 7 yearsmakes it clear that you should use "./assets" rather "./src/..." ... spent ages scratching my head on this one...