Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include images in an Angular library?

Tags:

angular

I created a simple Angular library and I want my library to also display an image. The problem is that if I include the image inside the module folder of my library and then refer to it from inside the module, I get 404 error. As far as I know, in an Angular project images must be placed inside /assets folder, but I really need to include this image in my library.

I placed the image inside the module folder and refered to it from a the .html file of my module component:<img src="myImage.png">, but it doesn't work.

like image 489
eli Avatar asked Oct 26 '17 08:10

eli


People also ask

How can we add image in angular?

Upload the simple image you need to create an application using the command " ng new my-app". You have to open your HTML component and then use the image tag and put the image with a name or with their proper path, such as: <img src=” ../../assets/images/image. jpg” class=” img1”> .

Where should I put images in angular?

In angular2, images and other media are by default fetched from the assets directory within your project folder(all other directories of the project are not public to the components by default) this can be changed by modifying angular-cli. json.

What is library project in angular?

Such a solution can be built as Angular libraries and these libraries can be published and shared as npm packages. An Angular library is an Angular project that differs from an application in that it cannot run on its own. A library must be imported and used in an application. Libraries extend Angular's base features.


1 Answers

There are several options here, none of which is perfect.

An image can be encoded with base64 to data URLs and used in a template inline:

<img src="data:image/jpg;base64,...">

Or be bound to component property that contains data URL:

<img [src]="imageFoo">

Images can be included alongside with the package, and a user can be instructed to copy them into website public directory. Since public path is unknown beforehand, the module can accept image path configuration with conventional forRoot method and use it for image path:

@Component({
  ...
  template: `<img src="{{imagesPath}}/foo.jpg">`
})
class SomeComponent {
  constructor(@Inject(IMAGES_PATH) public imagesPath: string) {}
}
like image 103
Estus Flask Avatar answered Nov 01 '22 01:11

Estus Flask