Angular 类型为'的参数;编号';不可分配给类型为';字符串';。将旧项目导入新项目时
将已完成的项目导入另一个项目后,我突然出现以下错误:Angular 类型为'的参数;编号';不可分配给类型为';字符串';。将旧项目导入新项目时,angular,typescript,compiler-errors,Angular,Typescript,Compiler Errors,将已完成的项目导入另一个项目后,我突然出现以下错误: ERROR in src/app/avior/users/users.component.html:43:79 - error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'. 43 <form [formGroup]="userForm" class="form-signin" (ngSubmit)="
ERROR in src/app/avior/users/users.component.html:43:79 - error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.
43 <form [formGroup]="userForm" class="form-signin" (ngSubmit)="onSubmit(selectedUser._id,this.userForm.value)">
ERROR in src/app/avior/users/users.component.html:59:83 - error TS2345: Argument of
type 'number' is not assignable to parameter of type 'string'.
59 <button class="btn btn-danger btn-block" type="submit" (click)="deleteUserBtn(selectedUser._id)">Löschen</button>
~~~~~~~~~~~~~~~~
我在旧项目中的原始代码功能齐全,这是如何出错的?
如果我错了,请更正,但this.userForm.value似乎是一个数字,需要将其转换为字符串
更新
在遵循建议后,我仍然会出现以下错误:
ERROR in src/app/avior/users/users.component.html:43:79 - error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.
43 <form [formGroup]="userForm" class="form-signin" (ngSubmit)="onSubmit(selectedUser._id,this.userForm.value)">
ERROR in src/app/avior/users/users.component.html:59:83 - error TS2345: Argument of
type 'number' is not assignable to parameter of type 'string'.
59 <button class="btn btn-danger btn-block" type="submit" (click)="deleteUserBtn(selectedUser._id)">Löschen</button>
~~~~~~~~~~~~~~~~
您的
onSubmit
方法采用2个参数
onSubmit(id:string,data:User)
id
您键入的是字符串
从您调用的onSubmit
模板中:
(ngSubmit)=“onSubmit(selectedUser.\u id,this.userForm.value)”
selectedUser.\u id
是一个号码
所以你得到了错误
“number”类型的参数不能分配给“string”类型的参数
因为您正在向接受字符串的方法传递一个数字
解决方案
更改您的onSubmit
方法,将id
作为编号键入:
onSubmit(id:number,data:User){//将字符串更改为number
来自您的评论
需要更改界面
,以反映数据的形状:
导出用户界面{
_id:string;//这应该是一个字符串
mandator?:数字;
loginId:string;
lastname:string;
名字:字符串;
密码:字符串;
电子邮件:字符串;
组?:字符串;
角色?:角色;
活动?:布尔值;
令牌?:字符串;
}
问题在于id包含字母字符,因此是一个字符串。换句话说,它应该是一个字符串。那么您的界面不正确,应该更改。将更新答案