简体   繁体   English

Angular 材料垫滑块无法与 Hammer.js 一起正常工作

[英]Angular material mat-slider not working properly with Hammer.js

I was stuck on this for a while so sharing this (as actively encouraged on SO) in case others are facing the same problem.我被困了一段时间,所以分享这个(正如在 SO 上积极鼓励的那样)以防其他人面临同样的问题。

The problem问题

In Angular when a custom Hammer.js config is created to prevent it blocking vertical page scroll and you are using Angular Material mat-slider , you might find the slider action not work smoothly, or allow the slider to be dragged. In Angular when a custom Hammer.js config is created to prevent it blocking vertical page scroll and you are using Angular Material mat-slider , you might find the slider action not work smoothly, or allow the slider to be dragged.

If similar to me, you followed suggestions online, you probably created a custom Hammer config in app.module.ts like this:如果和我类似,你遵循了网上的建议,你可能在app.module.ts中创建了一个自定义的 Hammer 配置,如下所示:

export class MyHammerConfig extends HammerGestureConfig {
  buildHammer(element: HTMLElement) {
    const mc = new Hammer(element, {
    touchAction: 'pan-y'
  });

  return mc;
 }
}

With the provider declaration:使用提供者声明:

{provide: HAMMER_GESTURE_CONFIG, useClass: MyHammerConfig}

The above config ensures only horizontal swipes are handled by Hammer, allowing vertical swipe events to reach the browser to scroll the page up and down.上述配置确保 Hammer 仅处理水平滑动,允许垂直滑动事件到达浏览器以上下滚动页面。
However, if you happen to use the Angular Material mat-slider then the custom Hammer config class can cause issues with the slider action.但是,如果您碰巧使用 Angular 材质垫滑块,那么自定义 Hammer 配置 class 可能会导致 slider 操作出现问题。

As mentioned earlier I will answer my own question below.如前所述,我将在下面回答我自己的问题。

Versions used:使用的版本:
Angular 8.0.0 Angular 8.0.0
Angular Material 8.0.0 Angular 材料8.0.0

To fix the problem with Angular Material mat-slider when a custom Hammer config is used, you simply extend from the GestureConfig class from "angular/material" instead of HammerGestureConfig:要在使用自定义 Hammer 配置时解决 Angular 材质垫滑块的问题,您只需从“角度/材质”而不是 HammerGestureConfig 扩展GestureConfig class:

import { GestureConfig } from '@angular/material';

export class MyHammerConfig extends GestureConfig  {
  buildHammer(element: HTMLElement) {
    return new GestureConfig({touchAction: 'pan-y'}).buildHammer(element);
  }
}

This should sort the slider problem.这应该对 slider 问题进行排序。

Credit to post in Github:致于 Github 中的帖子:
https://github.com/angular/components/issues/4278#issuecomment-399572805 https://github.com/angular/components/issues/4278#issuecomment-399572805

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

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