【问题标题】:How to globally set the preserveWhitespaces option in Angular to false?如何将Angular中的preserveWhitespaces选项全局设置为false?
【发布时间】:2018-02-11 14:27:43
【问题描述】:

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

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

【问题讨论】:

  • 所以你是说如果你在你的tsconfig.json 中设置angularCompilerOptions不会生效?你能显示一个minimal reproducible example 的设置吗?
  • @jonrsharpe 啊,就是这样。你能把你的评论变成答案吗?
  • @LazarLjubenović,你使用 Angular cli 吗?
  • @AngularInDepth.com 是的。
  • @LazarLjubenović,然后 jonsharpe 的回答将起作用

标签: angular


【解决方案1】:

为了在 AOT compile (ng serve --aot, ng build --prod) 中设置 Angular 编译器选项,您必须更改 tsconfig.app.json 以包含:

"angularCompilerOptions": {
  "preserveWhitespaces": true
},


为了在 JIT compile (ng serve) 中设置角度编译器选项,您必须更改 main.ts,特别是 bootstrapModule 调用:

platformBrowserDynamic().bootstrapModule(AppModule, {
  preserveWhitespaces: true
})
.catch(err => console.log(err));

【讨论】:

    【解决方案2】:

    默认情况下,从角度 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

    在 angular-cli repo 中也有 feature request

    【讨论】:

    • 你的意思是 tsconfig.app.json,因为你输入的是扩展 tsconfig.json
    • 工作正常,我的大模板渲染时间减少了 50%
    • 这里讨论了 preserveWhitespaces:false 的攻击性行为:github.com/angular/angular/issues/21049
    • 正在寻找一种快速重新放入真实空间的方法? {{ ' ' }} 是我发现的最安全、最干净的方式。
    • 我使用“ngPreserveWhitespace”作为我明确希望在其中包含空格的元素的指令:
      1 2
    猜你喜欢
    • 1970-01-01
    • 2020-02-08
    • 1970-01-01
    • 2015-11-28
    • 2023-01-31
    • 2017-06-16
    • 1970-01-01
    • 2022-06-13
    • 1970-01-01
    相关资源
    最近更新 更多