#Image optimization with NgOptimizedImage, ngSrc not loading image

8 messages · Page 1 of 1 (latest)

barren urchin
#

Hello everyone, I'm trying to optimize an angular website. My first task is image optimization. I read the documentation and guide for NgOptimizedImage here:
https://angular.io/guide/image-directive
Sorry if I misunderstood, but this is my understanding. I do not a loader (step 2) and I'm trying to load an image from my assets folder. So I've imported NgOptimizedImage from @angular/common and added it to imports in the base app.module.ts. On my component the image is loaded with

  <img ngSrc="../../../assets/img/calidad-catracha.webp" height="427" width="640" 
    fill   priority
    alt="Calidad Catracha Background Image"
    > 

This code does not load the image, it just renders the alt value as if the file missing, I also get no errors from console.

If i try to bind to the value, where imageSource = "../../../assets/img/calidad-catracha.webp"

  <img [ngSrc]="imageSource" height="427" width="640" 
    fill   priority
    alt="Calidad Catracha Background Image"
    > 

I get error "Can't bind to 'ngSrc' since it isn't a known property of 'img'"

If I just use src instead of ngSrc the image is loaded correctly.

I've also tried googling it without finding a mention of this error. I also asked chatGPT and it's not being any help either.

I'm the attaching the output of ng version and I'm marking it as beginner as I can't find other tag related to it. Excuse any mistakes, it's my first time working with optimization.

wet storm
barren urchin
wet storm
#

In which NgModule is the above component declared? Post its code (and tell us which component it is)

barren urchin
#

EDIT 1:
Adding the module where I'm both importing NgOptimizedImage and declaring my component. ALSO I can now bind to ngSrc but the image is still not loading

import { NgModule } from '@angular/core';
import { CommonModule, NgOptimizedImage } from '@angular/common';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { SwiperModule } from 'swiper/angular';

import { HomeComponent } from '../home/home.component';
import { CalidadCatrachaComponent } from '../home/calidad-catracha/calidad-catracha.component';
import { CategoriesComponent } from '../home/categories/categories.component';
import { WhyUsComponent } from '../home/why-us/why-us.component';
import { MostPopularComponent } from '../most-popular/most-popular.component';
import { CtaComponent } from '../home/cta/cta.component';
import { ContactComponent } from '../home/contact/contact.component';
import { HomeRoutingModule } from './home-routing.module';

@NgModule({
  declarations: [
    HomeComponent,
    CalidadCatrachaComponent, //This the component where I'm trying to load the image
    WhyUsComponent,
    CategoriesComponent,
    MostPopularComponent,
    CtaComponent,
    ContactComponent,
  ],
  imports: [
    CommonModule,
    FormsModule,
    ReactiveFormsModule,
    SwiperModule,
    HomeRoutingModule,
    NgOptimizedImage // Here is the import
  ]
})
export class HomeModule { }
wet storm
#

This looks correct. Try deleting the .angular directory and restarting ng serve.

quartz quest
#

If you use fill, avoid specifying height and width.

wet storm
#

Also, ../../../assets/img/calidad-catracha.webp should just be assets/img/calidad-catracha.webp