如何为 Angular 8 或 9 启用 Ivy?

Lin*_* Vu 23 angular

如何在 Angular 8 或 9 项目中启用 Ivy?

Ivy 是即将推出的 Angular 渲染引擎,它在不改变 Angular 项目当前代码库的情况下引入了许多不错的功能。

Cha*_*ghe 24

来源和更多信息通过这个

参考: https : //dzone.com/articles/how-to-upgrade-angular-packagesenable-ivy-compiler

你可以自动升级

npm i -g @angular/cli@latestng update
Run Code Online (Sandbox Code Playgroud)

或者在你的tsconfig.json文件中更新这个

{
  "compilerOptions": {
    "module": "esnext",
    // ...
  },
  "angularCompilerOptions": {
    "enableIvy": true,
    "allowEmptyCodegenFiles": true
  }
}
Run Code Online (Sandbox Code Playgroud)

然后你的angular.json文件

{
  "projects": {
    "your-project": {
      "architect": {
        "build": {
          "options": {
            ...
            "aot": true,
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)


Sta*_*tak 16

使用此官方指南。

在新项目中使用 Ivy:

要在启用 Ivy 的情况下启动新项目,请在 ng new 命令中使用 --enable-ivy 标志:

ng new shiny-ivy-app --enable-ivy
Run Code Online (Sandbox Code Playgroud)

新项目会自动为 Ivy 配置。具体来说,项目tsconfig.app.json文件中的 enableIvy 选项设置为 true 。

在现有项目中使用 Ivy:

要更新现有项目以使用 Ivy,请在项目的 .angularCompilerOptions 中设置 enableIvy 选项tsconfig.app.json

{
  "compilerOptions": { ... },
  "angularCompilerOptions": {
    "enableIvy": true
  }
}
Run Code Online (Sandbox Code Playgroud)

使用 Ivy 进行 AOT 编译速度更快,应该默认使用。在 angular.json 工作区配置文件中,将项目的默认构建选项设置为始终使用 AOT 编译。

{
  "projects": {
    "my-existing-project": {
      "architect": {
        "build": {
          "options": {
            ...
            "aot": true,
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

要停止使用 Ivy 编译器,请将 enableIvy 设置为 false in tsconfig.app.json,或将其完全删除。如果您之前没有,也请从您的默认构建选项中删除 "aot": true。

  • 使用 Angular 10,在这个答案发布几个月后:`Unknown option: 'enableIvy'` 天哪,我开始讨厌 Angular... (4认同)