如何在angular9中使用ngx-long-press处理长按?

问题描述 投票:0回答:1

我在使longPress在angular 9中工作时遇到问题。我已经在https://github.com/Gbuomprisco/ngx-long-press之后导入了模块,并且有一个简单的按钮

<button [longPress] (onRelease)="onLongPress()" > </button>

和一个简单的

onLongPress(){
    console.log("long press works");
  }

在我的ts文件中。但是,控制台日志永远不会显示。控制台中没有错误,什么也没有发生,所以我的问题是:该模块是否可以在Angular 9中使用?如果不是,那么在角度9中长按的最佳模块是什么?

angular angular9
1个回答
0
投票

添加longPress指令

import { Directive, ElementRef, EventEmitter, OnDestroy, OnInit, 

Output } from '@angular/core';
import { Clipboard } from '@ionic-native/clipboard';
import { Gesture } from 'ionic-angular/gestures/gesture';
import { Platform } from 'ionic-angular';
declare var Hammer;

@Directive({
  selector: '[longPress]'
})

export class PressDirective implements OnInit, OnDestroy {
  @Output() onLongPressEvent = new EventEmitter();
  el: HTMLElement;
  pressGesture: Gesture;

  constructor(el: ElementRef, private clipboard: Clipboard, private plt: Platform) {
    this.el = el.nativeElement;
    this.onLongPressEvent.emit();
  }

  ngOnInit() {
    if (this.plt.is('ios')) {
      this.pressGesture = new Gesture(this.el);
    } else {
      this.pressGesture = new Gesture(this.el,
        {
          recognizers: [
            [Hammer.Press, { time: 1000 }]
          ]
        });
    }
    this.pressGesture.listen();
    this.pressGesture.on('press', e => {
      this.clipboard.copy(e.target.textContent.trim());
      this.onLongPressEvent.emit();
    });
  }

  ngOnDestroy() {
    this.pressGesture.destroy();
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.