为什么Angular10会出现“Cannot set properties of undefined...”的错误?
Why the error " Cannot set properties of undefined..." appears in Angular 10?
我正在开发一个用户可以注册的应用程序,为此用户必须写入姓名、昵称、密码、身份证明类型等数据...
用户型号:
export class User{
id: string;
name: string;
nickname: string;
password: string;
docType: {
documentTypeId: number;
}
}
组件.ts:
public insertUser(event: Event): void{
event.preventDefault();
const u: User = new User();
u.id = this.form.value.id;
u.name = this.form.value.name;
u.nickname = this.form.value.nickname;
u.password = this.form.value.password;
u.docType.documentTypeId = Number(this.form.value.typeId); // the error occurs here
/*send to API, save to DB... */
}
所以,u.docType.documentTypeId
的值赋值时出现错误,我不知道我做错了什么。
这个有用吗:
u.docType = {
documentTypeId: Number(this.form.value.typeId);
}
我认为应该!
我正在开发一个用户可以注册的应用程序,为此用户必须写入姓名、昵称、密码、身份证明类型等数据...
用户型号:
export class User{
id: string;
name: string;
nickname: string;
password: string;
docType: {
documentTypeId: number;
}
}
组件.ts:
public insertUser(event: Event): void{
event.preventDefault();
const u: User = new User();
u.id = this.form.value.id;
u.name = this.form.value.name;
u.nickname = this.form.value.nickname;
u.password = this.form.value.password;
u.docType.documentTypeId = Number(this.form.value.typeId); // the error occurs here
/*send to API, save to DB... */
}
所以,u.docType.documentTypeId
的值赋值时出现错误,我不知道我做错了什么。
这个有用吗:
u.docType = {
documentTypeId: Number(this.form.value.typeId);
}
我认为应该!