我在MIN_PW組件的 之上定義了一個變數 ,export class MyComponent盡管智能感知在我的組件中識別了它,formBuilder但它沒有正確出現在 UI 中(UI 顯示“密碼必須至少包含字符”)。
最初我已經在其中定義了變數export class并且它正在作業,但我被建議設定它const并將其移動到組件上方。
組件檔案:
const MIN_PW = 12;
export class MyComponent implements OnInit {
myFormGroup = this.formBuilder.group({
password: new FormControl(null, [Validators.required, Validators.minLength(MIN_PW)]),
// etc
)
}
模板檔案:
<mat-form-field class="password-field">
<mat-error *ngIf="myFormGroup.controls['password'].invalid">Password must contain at least {{MIN_PW}} characters.</mat-error>
</mat-form-field>
uj5u.com熱心網友回復:
我建議將所有常量宣告在一個單獨的檔案中,您可以將其匯入到任何您想要的地方。
對我來說,以下方法對于您的情況似乎更直接:
- 在一個或多個 TS 檔案中定義和匯出常量。
- 使用 import { MY_CONSTANT } from '../constants/my-constant.ts' 匯入常量;無論您在哪里需要它們。
- 使用它們并快樂。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/375609.html
上一篇:使用Typescript將onClick函式作為道具發送到React中的組件
下一篇:打字稿中的小寫物件值?
