Angular 10 更严格的设置 --strict [英] Angular 10 Stricter Settings --strict
问题描述
在 Angular 10 中,您可以使用 ng new --strict
In Angular 10 you can create a new project using ng new --strict
启用此标志会使用一些新项目初始化您的新项目提高可维护性的设置,帮助您提前发现错误时间,并允许 CLI 对您的应用执行高级优化
Enabling this flag initializes your new project with a few new settings that improve maintainability, help you catch bugs ahead of time, and allow the CLI to perform advanced optimizations on your app
是否有将现有项目升级到严格模式的命令行,或者我只需要创建一个新项目然后从另一个项目复制粘贴文件?
Is there a command line that will upgrade an existing project to strict mode, or I just need to create a new project and then copy-paste the files from the other project?
推荐答案
基本上有一些变化:
tsconfig.base.json
获得一些新规则:
tsconfig.base.json
gets some new rules:
"compilerOptions": {
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true
},
"angularCompilerOptions": {
"strictInjectionParameters": true,
"strictTemplates": true
}
tslint.json
得到更新
"no-any": true
- 在您的
angular.json
中减少了捆绑预算大小:
- the bundle budget sizes are reduced in your
angular.json
:
"configurations": {
//...
[envName]: {
"budgets": [
{
"type": "initial",
"maximumWarning": "500kb",
"maximumError": "1mb",
},
{
"type": "anyComponentStyle",
"maximumWarning": "2kb",
"maximumError": "4kb",
},
]
}
}
以及在 angular.json
中的 projects.[projectName].schematics
路径中添加的原理图:
and a schematics addition to your projects.[projectName].schematics
path in the angular.json
:
schematics: {
"@schematics/angular:application": {
"strict": true
}
}
- 应用
package.json
使用sideEffects
属性更新:
- the app
package.json
gets updated with asideEffects
property:
sideEffects: false
有关其作用的解释,我可以向您推荐这个答案.
For an explanation what this does, I can refer you to this answer.
此 package.json
位于您的应用文件夹内.在使用 ng update
选项时,它应该与迁移一起添加到那里.如果你没有这个文件,你可以使用这个 模板
This package.json
is located inside your app folder. It should be added there with the migration while using the ng update
option. If you do not have this file, you can use this template
要转换当前的代码库,尤其是 tsconfig 更新和 no-any
会让您头疼.但是获得更好的(更少隐藏的错误)和更容易重构代码库绝对是值得的.
To convert a current codebase, especially the tsconfig updates and the no-any
will give you some headache to fix. But it will be definitely worth it to get a better (less hidden bugs) and easier to refactor codebase.
这篇关于Angular 10 更严格的设置 --strict的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!