【发布时间】:2021-11-23 18:04:44
【问题描述】:
我有以下用户类:
export class User{
id: number = 0;
fName: string = "";
lName: string = "";
constructor(){}
fullName() { return this.fName + ' ' + this.lName; }
email() { return this.fname + "@sof.com"}
}
我正在尝试创建一个表单,以便用户可以输入他们的信息。我想要一个双向数据绑定,所以我在 user-form.component.ts 中创建了一个 User 类的实例:
import { Component, OnInit } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { User } from '../user';
@Component({
selector: 'app-user-form',
templateUrl: './user-form.component.html',
styleUrls: ['./user-form.component.css']
})
export class UserFormComponent implements OnInit {
constructor() { }
usrData: User= {} as User;
ngOnInit(): void {
this.usrData = {
id: 123,
fName: "user",
lName: "name"
}
}
}
但是当我完成实例化时,usrData 带有红色下划线并给出以下错误:
输入 '{ id: number; f名称:字符串; lName:字符串; }' 缺少“用户”类型的以下属性:全名、电子邮件 ts(2739)
我在这里做错了什么?
提前致谢。
【问题讨论】:
标签: angular