📅  最后修改于: 2023-12-03 15:17:52.248000             🧑  作者: Mango
ng-lazyload-image is a simple and lightweight JavaScript library used to lazy-load images in web pages. It is built on top of Angular and provides a directive that lets you load images only when required, rather than at page load time. This can help to improve the performance of your web page by reducing the time it takes to load your images.
npm install --save ng-lazyload-image
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { NgLazyLoadImageModule } from 'ng-lazyload-image';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
NgLazyLoadImageModule.forRoot() // Import the NgLazyLoadImageModule
],
bootstrap: [AppComponent]
})
export class AppModule { }
<img ng-lazyload-image="https://example.com/image.jpg" alt="Image Description">
ng-lazyload-image provides various options to customize the library according to your preferences.
You can add a loading animation to your images while they are being loaded using the loadingIcon
option.
import { NgLazyLoadImageModule } from 'ng-lazyload-image';
@NgModule({
imports: [
NgLazyLoadImageModule.forRoot({
loadingIcon: 'https://example.com/loading.gif'
})
],
})
export class AppModule { }
You can set a placeholder image for your lazy-loaded images using the defaultImage
option.
import { NgLazyLoadImageModule } from 'ng-lazyload-image';
@NgModule({
imports: [
NgLazyLoadImageModule.forRoot({
defaultImage: 'https://example.com/placeholder.jpg'
})
],
})
export class AppModule { }
ng-lazyload-image is a powerful library that can significantly improve the performance of your web page by reducing image load times. It is easy to implement and customize, making it an excellent choice for both beginners and advanced users.