小编Rah*_*hav的帖子

在 angular 11 材质 UI 中,我收到错误 TS2531:对象可能在我的 html 模板中为“空”

我正在使用材料 UI 和反应形式。我的应用程序运行正常,我能够登录,但在我的终端窗口中,我收到错误 TS2531:对象可能为“空”。我附上了下面的错误屏幕截图以供参考,以及我的登录 html 模板和组件。

我的 login.component.html 代码是

<div class="example-container">
  <div class="form-container">
    <form [formGroup]="loginForm" (submit)="onSubmit()">
      <mat-form-field class="form-field" appearance="outline">
        <mat-label> E-mail
        </mat-label>
        <input matInput formControlName="email" required>
        <mat-error *ngIf="loginForm.controls.email.touched && loginForm.controls.email.invalid">
          <span *ngIf="loginForm.controls.email.errors.required">This field is mandatory.</span>
          <span *ngIf="loginForm.controls.email.errors.pattern">This field is invalid.</span>
        </mat-error>
      </mat-form-field>
      <mat-form-field class="form-field" appearance="outline">
        <mat-label> Password
        </mat-label>
        <input matInput formControlName="password" type="password">
        <mat-error *ngIf="loginForm.controls.password.touched && loginForm.controls.password.invalid">
          <span *ngIf="loginForm.controls.password.errors.required">This field is mandatory.</span>
        </mat-error>
      </mat-form-field>
      <button mat-raised-button color="primary" type="submit">Log In</button>
    </form>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

我的 login.component.ts 代码是

import { Component, OnInit } from …
Run Code Online (Sandbox Code Playgroud)

typescript angular angular11

0
推荐指数
1
解决办法
1716
查看次数

标签 统计

angular ×1

angular11 ×1

typescript ×1