3

I tried to implement the vertical swipe with Hammer.js and Angular 17 but it's not working.

I'm trying to do a tinder-like app, so I want to be able to swipe right, swipe left and swipe up. I found the hammer.js module to do this easily, and the horizontal swipe works correctly, however, the vertical swipe isn't recognized.

Here is my app.config.ts :

@Injectable()
export class HammerConfig {
overrides: any = {
swipe: { direction: DIRECTION_ALL },
}};

export const appConfig: ApplicationConfig = {
    providers: [
        provideRouter(routes),
        provideAnimations(),
        { provide: HAMMER_GESTURE_CONFIG, useClass: HammerConfig },
        importProvidersFrom(HammerModule),
    ]
};

Here is my swipe.component.html :


<div class="test"
    (swipedown)='startAnimation("swipedown")'
    (swipeup)='startAnimation("swipeup")'
    (swipeleft)='startAnimation("swipeleft")' 
    (swiperight)='startAnimation("swiperight")'
    >
    <p>{{ swipeEvent }}</p>
    
</div>

And finally my function startAnimation :


startAnimation(state: string) {
    this.swipeEvent = state;
    console.log('startAnimation', state);
}

If someone knows how to fix this, I will be so thankfull !

Thank you ! :)

1
  • If someone already did this with angular, it would help me a lot ! Thanks Commented Feb 14 at 16:25

1 Answer 1

1

this is a working solution for my Angular 17 standalone app

  1. Add "import 'hammerjs';" to your app.config.ts. This will import
  2. Import hamemr module importProvidersFrom(HammerModule),
  3. You don't need to override hammer gesture config for basic usage.
2
  • with import 'hammerjs', I immediately get "[vite] Internal server error: window is not defined at node_modules/hammerjs/hammer.js" How did you get around that? Using angular 17.3.1 Commented Apr 2 at 19:08
  • Ok, import the hammerjs code using as provider: { provide: HAMMER_LOADER, useValue: () => import('hammerjs') }, Commented Apr 3 at 8:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.