Skip to content

bug(hook): hook won't trigger on lazy-loaded routes #554

@DwieDima

Description

@DwieDima

I use the latest version of ionic/angular.

Here I want to add a FadeIn animation when an image is initially loaded. After loading the image, the FadeIn animation should not be executed again on RouteChange.

Currently the hook does not trigger at all (no animation visible, no console logs).

Expected: Hook should trigger

import { Injectable, NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouteReuseStrategy } from '@angular/router';

import { IonicModule, IonicRouteStrategy } from '@ionic/angular';
import {
  Attributes,
  IntersectionObserverHooks,
  LAZYLOAD_IMAGE_HOOKS,
} from 'ng-lazyload-image';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';

@Injectable()
class LazyLoadImageHooks extends IntersectionObserverHooks {
  public constructor() {
    super();
    console.log('SUPER');
  }

  public setLoadedImage(imagePath: string, attributes: Attributes): void {
    console.log('setLoadedImage');
    attributes.element.classList.add('animated-img');
    super.setLoadedImage(imagePath, attributes);
  }

  public finally(attributes: Attributes): void {
    console.log('finally');
    super.finally(attributes);
    setTimeout(() => {
    // timeout must match animation duration defined in global.scss
    // remove class when image loaded
    // otherwise fadeIn animation will trigger on every route change
    attributes.element.classList.remove('animated-img');
    }, 3000);
  }
}

@NgModule({
  declarations: [AppComponent],
  imports: [
    BrowserModule,
    IonicModule.forRoot({ mode: 'ios' }),
    AppRoutingModule,
  ],
  providers: [
    { provide: RouteReuseStrategy, useClass: IonicRouteStrategy },
    { provide: LAZYLOAD_IMAGE_HOOKS, useClass: LazyLoadImageHooks },
  ],
  bootstrap: [AppComponent],
})
export class AppModule {}

global.scss

.animated-img {
  animation: fadeIn 3s;
}

@keyframes fadeIn {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}

Reproduction:

https://stackblitz.com/edit/github-i3f4hm?file=src/app/app.module.ts

i suspect the issue has to do something with the routing of ionic ion-tabs, if you put the images outside of ion-tabs, everything works as expected.
Unfortunately I could not find a solution yet.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions