Could not find HammerJS in angular 6

I use angular-material in my project. I am getting this warnings:

Could not find HammerJS. Certain Angular Material components may not work correctly.

The "longpress" event cannot be bound because Hammer.JS is not loaded and no custom loader has been specified.

I know this is a duplicate of this question

In the answer they have mentioned that:

  1. We have to add "^2.0.8", to dependencies in package.json file.
  2. import 'hammerjs/hammer'; in polyfills.ts file.

In my case everything is fine, still I am getting the same warning in browser console.


Solution 1:

There are 2 ways to solve this problem:

  1. Either include the (main) import in your main module's file or polyfills.ts:

    import 'hammerjs';
    
  2. Or include the script from a CDN into your index.html file:

    <head>
      <!-- ... -->
      <script src="https://cdnjs.cloudflare.com/ajax/libs/hammer.js/2.0.8/hammer.min.js"></script>
      <!-- ... -->
    </head>
    

Solution 2:

Install with

npm install --save hammerjs

or

yarn add hammerjs

After installing, import it on your app's entry point (e.g. src/main.ts).

import 'hammerjs';

Angular Material Getting Started Guide