[英]Error property _id using Typescript in ANGULAR
I have this message Error in angular:我在 angular 中有此消息错误:
src/app/components/employee/employee.component.html:67:92 - error TS2345: Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
Type 'undefined' is not assignable to type 'string'.
67 <button class="btn btn-danger btn-sm mr-2 (click)="deleteEmployee(employee._id)">
src/app/components/employee/employee.component.ts:8:16
8 templateUrl: './employee.component.html',
Error occurs in the template of component EmployeeComponent
EmployeeComponent
的模板出现错误
Employee
Employee
Please a need know why this happened请需要知道为什么会这样
I think the value being passed in for _id
is "uninitialized" and therefore its value is undefined.我认为为
_id
传入的值是“未初始化的”,因此它的值是未定义的。 I can't tell without seeing more of your code.如果没有看到更多您的代码,我无法判断。 One way to fix this would be when you instantiate the employee, make sure
_id
is actually being set to a value, otherwise set it to ''
(blank string) and check that it has a value or not in your delete().解决此问题的一种方法是在您实例化员工时,确保
_id
实际上被设置为一个值,否则将其设置为''
(空白字符串)并检查它在您的 delete() 中是否有值。
Another option would be to edit your delete function另一种选择是编辑您的删除 function
deleteEmployee(_id: string | undefined){
if(_id) { //Checks if the passed variable has a value
...
}
else {
// Error
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.