【问题标题】:How to add Web Animations API polyfill to an Angular 2 project created with Angular CLI如何将 Web Animations API polyfill 添加到使用 Angular CLI 创建的 Angular 2 项目中
【发布时间】:2016-08-08 18:53:40
【问题描述】:

Angular 2 animations documentation 指的是 Web Animations API polyfill,用于不支持原生浏览器的浏览器。

将这个 polyfill 添加到使用 Angular CLI 创建的 Angular 2 项目的正确方法是什么?

(我正在使用 angular-cli: 1.0.0-beta.10)

没有运气,我尝试了这里提到的想法和解决方案:

我通过 NPM 下载并添加到 system-config.ts。我相信这符合推荐的内容,但 polyfill 没有被加载(我可以判断,因为动画在 Safari 中不起作用)。

我只是通过在 index.html 中包含 polyfill 来实现这一点,我知道这不是正确的方法:

  <script src="https://rawgit.com/web-animations/web-animations-js/master/web-animations.min.js"></script>

我将在此处添加任何可能有助于澄清我的问题的详细信息,但如果您需要查看代码,我在 Github 上有:

https://github.com/cmermingas/connect-four

提前致谢!

【问题讨论】:

标签: angular angular-cli


【解决方案1】:

使用更新的 Webpack 版本的 Angular CLI 添加 polyfill 更容易:

  1. 使用npm install web-animations-js --save安装

  2. 添加到polyfills.ts:require('web-animations-js/web-animations.min');

如果我这样做 import 'web-animations-js/web-animations.min'; 也有效

【讨论】:

  • 我在 polyfills.ts 中为 require('smoothscroll-polyfill').polyfill(); 尝试了这个,但我一直收到错误消息 D:/Projects/app/src/polyfills.ts (70,1): Cannot find name 'require'.。知道如何包含这个 polyfill 吗?
  • @mtpultz 目前,polyfill.ts 脚本有一个正确导入的注释行。
  • 此解决方案目前不适用于 Angular 2 项目。
  • 刚刚尝试使用具有相同答案的最新 cli 版本复制 angular 4 中的问题。动画问题仍然存在。我还缺少其他步骤吗?
  • @Safal 我的 polyfill.ts 由 angular cli 1.2.6 生成包含这一行:// import 'web-animations-js'; 所以你需要通过 @Trenton 提到的 import 替换 require
【解决方案2】:

Angular 6 注意 - 不再需要 Polyfill :-)

动画性能改进

我们更新了我们的实施 动画不再需要 web 动画 polyfill。这 意味着您可以从应用程序中删除此 polyfill 并保存 大约 47KB 的包大小,同时增加动画 同时在 Safari 中表现出色。

:-)

https://blog.angular.io/version-6-of-angular-now-available-cc56b0efa7a4

【讨论】:

  • 注意:据我所知,ng update 逻辑目前没有删除它,所以你必须自己卸载它。
  • 那为什么Safari上的动画功能不等同于Chrome呢?
  • @LoveenDyall 你能澄清你的意思吗?动画是否无法在 Safari 上运行或表现不同?或者你的意思是为什么它最初不起作用 - 在这种情况下,它可能只是与尚未实施的标准有关 - 但我并没有真正跟上这一点,只做非常基本的动画。
【解决方案3】:

在 Angular 4 中,只需按照以下步骤操作:

  1. 添加 web-animations-js 作为依赖:

    npm install web-animations-js

  2. 并取消注释polyfils.ts中的以下行

    导入“网络动画-js”; // 运行npm install --save web-animations-js。

【讨论】:

  • Dezza,我正在使用 webpack 版本的 angular cli/angular4,我得到 SCRIPT5007: Object expected; IE9 中的 web-animations.min.js (15,23868) 错误。遵循相同的步骤。
  • 不确定是否诚实。也许尝试删除 node_modules、'npm cache clean --force'、'npm install' 来重置所有内容
【解决方案4】:

我猜你已经完成了最多的步骤。只是为了让它完整:

1) 运行 npm install web-animations-js --save

2) 将 web-animation-js 添加到 angular-cli-build.js 以明确它是一个供应商包:

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

3)配置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'}
};

这里的重点是我们需要告诉system-js这个包的主文件是什么。 System-js 无法从 package.json 文件中获取此信息 - system-js 期望该文件是 index.js。但事实并非如此。它是 web-animations.min.js。

4) 在您的 main.ts 文件中添加:

import 'web-animations-js';

5) 停止并重新启动所有使用 angular-cli-build.js 的进程(ng 服务器、ng 测试等)。

现在系统 js 将加载 web-animation.js polyfill。

【讨论】:

  • 感谢您的详细解答。我进行了更改,但在浏览器中出现 404 错误:GET http://localhost:4200/vendor/web-animation-js/web-animations.min.js 404 (Not Found) Unhandled Promise rejection: Error: XHR error (404 Not Found) loading http://localhost:4200/vendor/web-animation-js/web-animations.min.js ... - 项目中有一个vendor 目录但web-animations-js 不在其中(它仅在node_modules 中)。
  • 你确定你已经完成了第 2 步吗?这确保在您的 dist/vendor 文件夹中存在 web-animations 文件夹。另外:您需要停止并启动所有使用 angular-cli-build.js - ng server, ng test, ...的进程
  • 停止并重新启动进程就可以了。非常感谢!
  • 我没有找到 angulr-cli-build.js 文件,我只有 angular-cli.ts,我在哪里可以找到它?如果是同一个文件,我必须在哪里添加 vendorNpnFiles 数组?
  • @mautrok 我认为这个公认的答案有点过时了,因为 Angular-cli 现在使用 webpack 而不是 system.js 进行构建。如下所示,您需要使用 npm 将其安装为依赖项,然后将其导入 polyfills.ts 这在真实浏览器中对我来说很好,但目前使用 PhantomJS 会导致错误
【解决方案5】:

我只是用 Visual Studio + gulp 完成的。

  1. 将此行添加到packages.json:

    "web-animations-js": "^2.2.2"

这将确保包被下载到 node_modules 文件夹。

  1. 将此行添加到 gulp.src 数组中的 gulpfile.js 中:

    'web-animations-js/web-animations.min.js'

这将确保在每次 gulp 构建期间将文件复制到 libs 文件夹(或您指定的任何文件夹)。

  1. 这就是你在 TypeScript 中导入它的方式:

    import 'libs/web-animations-js/web-animations.min.js';

【讨论】:

    【解决方案6】:

    您可以按照以下 3 个步骤操作:

    1- 运行这个命令: npm i -D web-animations-js

    2- 将这些代码行添加到“angular.json”文件中。

    {
    ...
    "projects:" {
      ...
      "architect": {
        ....
        "build": {
          ...
          "scripts": [
                  {
                    "input": "node_modules/web-animations-js/web-animations.min.js",
                    "inject": false,
                    "bundleName": "web-animations-js"
                  }
                ],
    

    第三,将这几行代码添加到“ployfills.ts”文件中。

    if (!('animate' in document.documentElement) || (navigator && /iPhone OS (8|9|10|11|12|13)_/.test(navigator.userAgent))) {
      const script = document.createElement('script');
      script.src = 'web-animations-js.js';
      document.head.appendChild(script);
    }
    

    请注意,我已将代码括在 if 语句中,因为此代码会在旧的 iOS 设备上产生问题,通常在 iPhone 6 和 iOS 12 中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-28
      • 1970-01-01
      • 2020-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多