Using images from assets in an Angular library html template

2,105 views
Skip to first unread message

Terry Lacy

unread,
Sep 17, 2020, 8:08:13 AM9/17/20
to Angular and AngularJS discussion

I've put some images in my Angular library project under the /src/assets/images folder.

Now I'd like to link to images from a component html template in the library using "<img href='...'>", but I haven't found a link that works. Everything I've tried gives me a 404.

I'd like it to work both when testing my library from within an app in the same workspace, and when the library is installed in an app from the npm package.

Thanks,
Terry

bastien lemaire

unread,
Sep 17, 2020, 8:37:33 AM9/17/20
to ang...@googlegroups.com
Libraries do not support exporting assets such as images. You can however use this customer builder to copy your assets in the output directory of your library: https://www.npmjs.com/package/@linnenschmidt/build-ng-packagr

Watch out you need to copy the assets in all output folders: 

image.png

Bastien Lemaire


--
You received this message because you are subscribed to the Google Groups "Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/angular/e42ea554-02f1-423f-b941-2676404f1023n%40googlegroups.com.

Terry Lacy

unread,
Sep 17, 2020, 12:42:46 PM9/17/20
to Angular and AngularJS discussion
Sorry, I should've said what version of Angular I'm using. I'm on Angular 10. As of Angular 9, assets are automatically copied to the dist folder of the library by ng-packagr. That part is working fine.

So, I'm just wondering if there's a way to link to them (e.g. from an img element) from a library component's html template.

Thanks,
Terry
Reply all
Reply to author
Forward
0 new messages