angular - 类型“X”不能分配给角度 4 中的类型“Y”
问题描述
很抱歉提出这个问题,但我需要有人帮助解决它。
我尝试的是我的 Angular 4 应用程序中有一个模型类,我想将类型分配给组件中的一个属性,但它显示错误为
错误 :
[ts]
Type '{ gender: null; mTitle: null; mFName: null; mLName: null; mEmail: null; mPassword: null; mDOB: Da...' is not assignable to type 'User'.
Types of property 'mDOB' are incompatible.
Type 'DateConstructor' is not assignable to type 'Date'.
Property 'toDateString' is missing in type 'DateConstructor'.
(property) CreateaccountComponent.mUser: User
User.ts(模型类)
export class User {
gender :string;
mTitle: string;
mFName: string;
mLName: string;
mEmail: string;
mPassword: string;
mDOB: Date;
mNewsLetter: string;
mSpecialOffer: string;
}
零件
import { User } from '../../User';
export class CreateaccountComponent implements OnInit {
mUser: User = {
gender: null,
mTitle: null,
mFName: null,
mLName: null,
mEmail: null,
mPassword: null,
mDOB: Date,
mNewsLetter: null,
mSpecialOffer: null,
};
constructor(private CartdataService: CartdataService, private router: Router, private http: HttpClient, ) {
this.router.events.subscribe(() => window.scrollTo(0, 0));
}
ngOnInit() { }
submitForm(userForm: NgForm): void {
}
}
这里 mUser 下划线为红色并显示上述错误。
谁能帮我解决这个问题。
解决方案
在初始化阶段,您将mDOB
具有名称的函数分配给属性Date
(所以为什么错误是关于Type 'DateConstructor' is not assignable to type 'Date'
.),而不是实例。指定默认日期或null
.
mUser: User = {
gender: null,
mTitle: null,
mFName: null,
mLName: null,
mEmail: null,
mPassword: null,
mDOB: new Date, // <----- new Date or null
mNewsLetter: null,
mSpecialOffer: null,
};
推荐阅读
- reactjs - 无法解析另一个文件中的 Typescript 模块
- amazon-web-services - 如果我停止付款,AWS S3 存储桶会怎样?
- amazon-web-services - 如何在没有 Route 53 的情况下运行 CloudFront
- laravel - Laravel 迁移未连接 AWS-RDS postgres(找不到驱动程序...)。PDO 已安装。如何解决?
- html - 遇到额外的小断点时忽略旋转
- .net - 使用 zip 文件作为文件存储库或“数据库”
- r - 闪亮的应用程序,允许用户单击网格中的多个图像并捕获单击的值
- python - 是否有删除这么多数据帧行以便只剩下 10 行的功能?
- r - 将列更改为因子会更改列中的字符串
- pyomo - 找不到求解器“glpk”所需的“glpsol”可执行文件