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
中减小:
"configurations": {
//...
[envName]: {
"budgets": [
{
"type": "initial",
"maximumWarning": "500kb",
"maximumError": "1mb",
},
{
"type": "anyComponentStyle",
"maximumWarning": "2kb",
"maximumError": "4kb",
},
]
}
}
以及对 projects.[projectName] .schematics
路径中的原理图的补充,位于 angular.json
中:
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屋!