【问题标题】:Angular 10 Stricter Settings --strictAngular 10 更严格的设置 --strict
【发布时间】:2020-06-26 07:53:30
【问题描述】:

在 Angular 10 中,您可以使用 ng new --strict 创建一个新项目

启用此标志会使用一些新的 提高可维护性的设置,帮助您提前发现错误 时间,并允许 CLI 对您的应用执行高级优化

是否有命令行可以将现有项目升级到严格模式,或者我只需要创建一个新项目,然后从其他项目复制粘贴文件?

【问题讨论】:

    标签: angular angular-cli


    【解决方案1】:

    基本上有几件事发生了变化:

    1. tsconfig.base.json 获得一些新规则:
    "compilerOptions": {
      "strict": true,
      "forceConsistentCasingInFileNames": true,
      "noFallthroughCasesInSwitch": true
    },
    "angularCompilerOptions": {
      "strictInjectionParameters": true,
      "strictTemplates": true
    }   
    
    1. tslint.json 得到更新
    "no-any": true
    
    1. 您的 angular.json 中的捆绑包预算大小已减少:
    "configurations": {
      //...
      [envName]: {
        "budgets": [
          {
            "type": "initial",
            "maximumWarning": "500kb",
            "maximumError": "1mb",
          },
          {
            "type": "anyComponentStyle",
            "maximumWarning": "2kb",
            "maximumError": "4kb",
          },
        ]
      }
    }
    

    以及在angular.json 中的projects.[projectName].schematics 路径中添加的示意图:

    schematics: {
      "@schematics/angular:application": {
        "strict": true
      }
    }
    
    1. 应用程序package.json 使用sideEffects 属性进行更新:
    sideEffects: false
    

    有关此功能的解释,我可以将您推荐给this 答案。

    这个package.json 位于您的应用文件夹中。在使用 ng update 选项时,它应该与迁移一起添加到那里。如果你没有这个文件,你可以使用这个template

    要转换当前的代码库,尤其是 tsconfig 更新和no-any 会让您有些头疼的问题需要修复。但是获得更好(隐藏的错误更少)和更容易重构代码库绝对值得。

    【讨论】:

    • 所以,没有迁移现有代码?我们必须手动更改吗?这就是全部吗?
    • @BlindDespair 我做了更多的挖掘,发现了更多的变化。
    • @PoulKruijt 很好的答案!你能改正sideEfects: false中的错字吗,人们很可能会复制粘贴。
    • 我没有 tsconfig.base.json。但我确实有 tsconfig.app.json 和 tsconfig.json。是一样的吗?
    • @ar099968 当然,您可以更新tslint.json,而不是"no-any": true,您可以更新"no-any": { "severity": "warning" }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-10
    • 1970-01-01
    • 2016-09-09
    • 2023-02-24
    • 1970-01-01
    • 1970-01-01
    • 2014-11-03
    相关资源
    最近更新 更多