简体   繁体   English

使用 touchstart 和/或单击(鼠标按下)Angular 6

[英]Using touchstart and/or click (mousedown) Angular 6

Issue:问题:

I have a problem where in the whole application where click event is used.我在使用单击事件的整个应用程序中遇到了问题。 The app will be used on both mobile and web.该应用程序将在移动和网络上使用。 I am using Angular 6.我正在使用 Angular 6。

Every time you click on a button or link on the browser on my desktop it works on first click, but on mobile the click doesn't work sometimes.每次您在我桌面上的浏览器上单击按钮或链接时,它都会在第一次单击时起作用,但在移动设备上,单击有时不起作用。 correct me if I'm wrong, but I believe people refer this as the ghost click.如果我错了,请纠正我,但我相信人们将此称为幽灵点击。

I thought that this was the 300ms delay, but I have tried using hammerjs's tap and tried fastclick instead and it seems like its not the issue.我认为这是 300 毫秒的延迟,但我尝试使用hammerjs 的 tap 并尝试使用 fastclick,这似乎不是问题。

I have tried using touchstart in html instead of click/tap and it seems to get rid of the issue.我尝试在 html 中使用 touchstart 而不是 click/tap,它似乎解决了这个问题。

Is there a way to bind mousedown and touchstart to each other?有没有办法将 mousedown 和 touchstart 相互绑定? is there a way to use just click/mousedown on desktop and touchstart on mobile?有没有办法在桌面上使用单击/鼠标按下并在移动设备上使用 touchstart?
What other ways can I go about fixing this?我还有什么其他方法可以解决这个问题?

If you are using Angular 6, Be default it internally uses hammerjs library to handle touch gesture events.如果您使用的是 Angular 6,默认情况下它在内部使用hammerjs 库来处理触摸手势事件。 Also it removes 300ms delay for double tap.它还消除了双击的 300 毫秒延迟。 Here is the URL that explains more on Touch Gesture in Angular.这是解释更多关于 Angular 中的触摸手势的 URL。 https://blog.angularindepth.com/gestures-in-an-angular-application-dde71804c0d0 https://blog.angularindepth.com/gestures-in-an-angular-application-dde71804c0d0

Try in your template:在您的模板中尝试:

<div
  (touchmove)="touchMoving($event)"
  (touchstart)="touchStart($event)"
  (touchend)="touchEnd($event)"
>

In your component you can now use the $event data:在您的组件中,您现在可以使用 $event 数据:

touchMoving($event) {
  console.log($event);
}

I have found that you should ALWAYS use (mousedown) instead of (click) for buttons in Angular if its a mobile app.我发现如果 Angular 是一个移动应用程序,你应该总是使用 (mousedown) 而不是 (click) 按钮。 With click the event sometimes will not register due to DOM issues.由于 DOM 问题,单击事件有时不会注册。 For best performance just use mousedown.为了获得最佳性能,只需使用 mousedown。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM