angular - 如何将 Angular 中的 preserveWhitespaces 选项全局设置为 false?

标签 angular

自版本 5 的测试版之一以来,Angular 有了一个新的编译器选项,preserveWhitespacesCompilerOptions type alias 中提到了该属性在文档中。 Component 装饰器的文档描述了 its usage ,并提到版本 5 中的默认值为 true(不删除空格)。

我看过 the PR ,但从一些测试中我可以看出,使用它的唯一方法是为每个 @Component 元数据提供 preserveWhitespace。我如何才能将它全局设置为 false,对于 所有 组件,然后将它设置为 true 仅对于某些组件?

最佳答案

从 angular 6 开始默认为 false

目前,在 JIT 模式下,我们可以将其设置为 CompileOptions 的一部分:

ma​​in.ts

platformBrowserDynamic().bootstrapModule(AppModule, { preserveWhitespaces: false });

对于aot我们必须添加这个选项

tsconfig.app.json

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/app",
    "baseUrl": "./",
    "module": "es2015",
    "types": []
  },
  "exclude": [
    "test.ts",
    "**/*.spec.ts"
  ],
  "angularCompilerOptions": {
    "preserveWhitespaces": false
  }
}

Angular-cli@1.4.5 example 哪里可以找到对应的commit

还有feature request在 angular-cli 仓库中。

关于angular - 如何将 Angular 中的 preserveWhitespaces 选项全局设置为 false?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46018599/

相关文章:

angular - 从 DOM 元素获取 ComponentRef

angular - Angular 测试期间 Karma 的反向代理配置

angular - 如何创建与 Angular 4 兼容的模态弹出窗口

angular - div 上的 RouterLink 与 @angular/router 3.0.0-alpha.3

javascript - Angular2使用指令动态插入html

http - Angular2/错误 : Collection not found

angular - 在 Angular TestBed 中模拟 Firestore 集合

angular - 在 Angular 指令上指定主体元素类型

php - Angular 6 .post .subscribe插入记录两次

angular - angular4 应用程序中输入的最小值和最大值