
[英]Argument of type 'string' is not assignable to parameter of type 'number'
[英]Argument of type 'number' is not assignable to parameter of type 'string'. when importing old project into a new one
将我完成的项目导入另一个项目后,我突然收到此错误:
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)">
我的 onSubmit 函数如下所示:
onSubmit(id: string, data: User) {
this.aviorBackend.putUser(id, data)
.subscribe(
res => {
console.log('Benutzer erneuert.');
console.log(id);
console.log(data);
console.log(res);
this.selectedUser = res;
},
error => {
console.log('Error while deleting user: ', error);
}
);
}
我在旧项目中的原始代码功能齐全,这是怎么回事? 如果我错了,请纠正我,但 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>
~~~~~~~~~~~~~~~~
更新 2用户界面:
import { Role } from './role';
// was class and not interface!
export interface User {
_id: number;
mandator?: number;
loginId: string;
lastname: string;
firstname: string;
password: string;
eMail: string;
group?: string;
role?: Role;
active?: boolean;
token?: string;
}
您的onSubmit
方法需要 2 个参数。
onSubmit(id: string, data: User)
您输入的id
作为string
。
从您调用onSubmit
的模板中:
(ngSubmit)="onSubmit(selectedUser._id,this.userForm.value)"
selectedUser._id
是一个number
所以你得到了错误
“数字”类型的参数不能分配给“字符串”类型的参数
因为您将数字传递给接受字符串的方法。
更改您的onSubmit
方法以将id
为number
:
onSubmit(id: number, data: User) { // changed string to number
从你的评论
需要更改interface
以反映数据的形状:
export interface User {
_id: string; // this should be a string
mandator?: number;
loginId: string;
lastname: string;
firstname: string;
password: string;
eMail: string;
group?: string;
role?: Role;
active?: boolean;
token?: string;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.