Javascript 无法在angular6中设置数组属性
我需要使用以下代码在angular 6中设置数组属性:Javascript 无法在angular6中设置数组属性,javascript,angular,typescript,angular6,Javascript,Angular,Typescript,Angular6,我需要使用以下代码在angular 6中设置数组属性: this.addupdate.roleids=this.selectedRole; 但它告诉我这个错误: 错误类型错误:无法设置未定义的属性“roleids” 在AccessLevelComponent.push../src/app/admin/admin/dashboard/role/access-level/access-level.component.ts.AccessLevelComponent.AddRoleClaim(acce
this.addupdate.roleids=this.selectedRole;
但它告诉我这个错误:
错误类型错误:无法设置未定义的属性“roleids”
在AccessLevelComponent.push../src/app/admin/admin/dashboard/role/access-level/access-level.component.ts.AccessLevelComponent.AddRoleClaim(access-level.component.ts:60)
和我的界面:
export interface IAddorupdateRole {
roleids:string[];
roleid:number;
}
这是我的代码:
public AddRoleClaim(){
console.log("enter AddRoleClaim.Ts");
this.addupdate.roleids=this.selectedRole;
this.addupdate.roleid=this.roleId;
this.roleService.AddOrUpdateRoleCalim(this.addupdate).subscribe((data)=>
{
console.log("seccess" + data);
}
);
}
有什么问题吗?如何解决这个问题?
roleids
可以通过this.addupdate
访问,但显然this.addupdate
未定义。可能还没有初始化
尝试初始化this.addupdate
到ngOnInit
中的基本对象,如下所示:
addupdate: IAddorupdateRole;
ngOnInit() {
this.addupdate = {
roleids = [],
roleid = 0
}
}
这是给你的裁判的一封信
addupdate: IAddorupdateRole;
ngOnInit() {
this.addupdate = {
roleids = [],
roleid = 0
}
}