我正在使用材料 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)