嚴格模式
Strict mode
建立新工作區或應用程式時,可以選擇使用 --strict
標誌以嚴格模式建立它們。
When you create a new workspace or an application you have an option to create them in a strict mode using the --strict
flag.
啟用此標誌將使用一些新設定來初始化新的工作區或應用程式,這些設定可以改善可維護性,幫助你提前發現錯誤。此外,使用這些更嚴格設定的應用程式更容易進行靜態分析,這可以幫助 ng update
命令在更新到 Angular 的將來版本時更安全,更準確地重構程式碼。
Enabling this flag initializes your new workspace or application with a few new settings that improve maintainability, help you catch bugs ahead of time. Additionally, applications that use these stricter settings are easier to statically analyze, which can help the ng update
command refactor code more safely and precisely when you are updating to future versions of Angular.
具體來說, strict
標誌執行以下操作:
Specifically, the strict
flag does the following:
在 TypeScript 中啟用
strict
模式,以及 TypeScript 團隊建議的其他嚴格性標誌。具體而言,forceConsistentCasingInFileNames
、noImplicitReturns
、noFallthroughCasesInSwitch
。Enables
strict
mode in TypeScript, as well as other strictness flags recommended by the TypeScript team. Specifically,forceConsistentCasingInFileNames
,noImplicitReturns
,noFallthroughCasesInSwitch
.開啟 Angular 編譯器的嚴格性標誌
strictTemplates
、strictInjectionParameters
和strictInputAccessModifiers
。Turns on strict Angular compiler flags
strictTemplates
,strictInjectionParameters
andstrictInputAccessModifiers
.打包尺寸預算減少了約 75%。
Bundle size budgets have been reduced by ~75%.
你可以在工作區一級和專案一級應用這些設定。
You can apply these settings at the workspace and project level.
要使用嚴格模式建立新的工作空間和應用程式,請執行以下命令:
To create a new workspace and application using the strict mode, run the following command:
ng new [project-name] --strict
要在現有的非嚴格工作空間內以嚴格模式建立新應用程式,請執行以下命令:
To create a new application in the strict mode within an existing non-strict workspace, run the following command:
ng generate application [project-name] --strict