Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

App won't recognize gestures, like pan, using Hammer.JS in Angular 11

I can't recognize any gestures, such as swipe, pan, in my Angular app using Hammer.JS. It's set up like this:

package.json:

"@angular/core": "11.0.5",
"@angular/platform-browser": "~11.0.5",
"@angular/platform-browser-dynamic": "~11.0.5",
"hammerjs": "^2.0.8",
"zone.js": "~0.10.3"

app.module.ts:

import { NgModule } from '@angular/core';
import { BrowserModule, HammerModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';


@NgModule({
  imports:      [ HammerModule, BrowserModule, BrowserAnimationsModule, FormsModule ],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ],
})
export class AppModule { }

main.ts:

import 'hammerjs';

app.component.ts:

onPan(evt) {
    console.log("onPAN");
}

app.component.html:

<div class="gesture__zone" (pan)="onPan($event)">
  <h1>Pan Gesture</h1>
</div>

I tested 'mousedown', it works well. Just any gestures in hammerJs won't work.

Is there something wrong with my config? Can anyone please help me to find the problem? Thanks a lot.

like image 453
Tao Avatar asked Jan 24 '23 14:01

Tao


2 Answers

you should imports HammerModule after BrowserModule like this:

import { NgModule } from '@angular/core';
import { BrowserModule, HammerModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform- 
browser/animations';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';


@NgModule({
  imports:      [ BrowserModule, BrowserAnimationsModule, HammerModule, 
   FormsModule ],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ],
})
export class AppModule { }
like image 153
lin liu Avatar answered Jan 31 '23 07:01

lin liu


I've managed to get this work on Angular 11 only by installing hammerjs manually with:

npm install hammerjs --save

app.module.ts

import { BrowserModule, HammerModule, HammerGestureConfig, HAMMER_GESTURE_CONFIG } from '@angular/platform-browser';
import * as Hammer from 'hammerjs';

export class HammerConfig extends HammerGestureConfig {
  overrides = {
    swipe: { direction: Hammer.DIRECTION_ALL },
  };
}

@NgModule({
  declarations: [....],
  imports: [...],
  providers: [{
    ...
    provide: HAMMER_GESTURE_CONFIG,
    useClass: HammerConfig
  }]

I know this should work without

like image 24
Csaba Avatar answered Jan 31 '23 08:01

Csaba