How to add Web Animations API polyfill to an Angular 2 project created with Angular CLI

后端 未结 5 1469
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-30 10:17

The Angular 2 animations documentation refers to the Web Animations API polyfill for browsers that don\'t support the native one.

What\'s the proper

5条回答
  •  死守一世寂寞
    2020-11-30 10:28

    I guess you have already done the most steps. Just to make it complete:

    1) run npm install web-animations-js --save

    2) add web-animation-js to angular-cli-build.js to make clear it is a vendor package:

    return new Angular2App(defaults, {
        vendorNpmFiles: [
          'systemjs/dist/system-polyfills.js',
           ...
          'web-animations-js/**/*'
        ]
    });
    

    3) Configure system-js (system-config.ts)

    /** Map relative paths to URLs. */
    const map: any = {
      'web-animations-js': 'vendor/web-animations-js'
    };
    
    /** User packages configuration. */
    const packages: any = {
      'web-animations-js': {main: 'web-animations.min.js'}
    };
    

    The main point here is that we need to tell system-js what the main file of this package is. System-js is not able to get this information from the package.json file - system-js expects the file is index.js. But it is not. It is web-animations.min.js.

    4) In your main.ts file add:

    import 'web-animations-js';
    

    5) Stop and restart all processes that are using the angular-cli-build.js (ng server, ng test, etc).

    Now system js will load the web-animation.js polyfill.

提交回复
热议问题