Angular 5 - Storing images inside the component folder

I know that the normal thing to do due to angular having the assets folder as the storage place is to store images inside the asset's folder in the project root.

I am thinking creating re-usable components and for portability issues I would like to have everything which belongs to a specific component inside it's own folder.

For example if I great an assets folder inside the component's folder and then try to reach an image from my mycomponent.component.html

<img src="./assets/myimage.jpg" alt="" />

Open in new window


If I do the above the image with not appear.

Does anyone know a workaround for this?
LVL 1
error2013Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
If you are using webpack you can use require(path/to/image) assign that to a component variable and then do
<img [src]="nameofvariable">

Open in new window

However this is not really the right way of doing it and (as of writing) won't work with a CLI buid.
There is some debate around this issue here
https://github.com/angular/angular-cli/issues/3292
and here
https://github.com/angular/angular-cli/issues/3415
But no real resolution - the gist being to change behaviour on the <img> would break existing applications.

There is merit in having the images with the component - but maybe a compromise for now is to have a component folder in the assets folder.

One could argue that to move a component would require moving its component file + its corresponding asset folder. Not as tightly coupled as you would like but with a proper convention in place moving components remains a simple task.
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
error2013Author Commented:
Sorry, typo...now appear....was mean't to be: not appear.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.