为什么我Cannot read property 'UserName' of undefined
在我的组件中定义它时总是出现错误。在 html 代码中使用“elvis 运算符”并不能解决问题。请查看我在何处收到此错误的代码:
用户模型.ts
export class User {
UserName: string;
Password: string;
Email: string;
FirstName: string;
LastName: string;
}
注册.component.ts
export class SignUpComponent implements OnInit {
user: User;
constructor() { }
ngOnInit() {
}
}
注册.component.html
<form class="col s12 white" #userRegistrationForm="ngForm">
<div class="row">
<div class="input-field col s6">
<input class="validate" type="text" name="UserName" #UserName="ngModel" [(ngModel)]="user.UserName" required minlength="3">
<label>Username</label>
</div>
<div class="input-field col s6">
<input type="password" name="Password" #Password="ngModel" [(ngModel)]="user.Password">
<label>Password</label>
</div>
</div>
<div class="row">
</div>
</form>
使用 elvis 运算符: <input class="validate" type="text" name="UserName" #UserName="ngModel" [(ngModel)]="user?.UserName" required minlength="3">
好像你正在使用export class User ...
你可以简单地做user: User = new User();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句