在 ANGULAR 中使用 Typescript 时出现错误 属性 _id
Error property _id using Typescript 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',
组件模板出错EmployeeComponent
型号:Employee
服务 :
JavaScript 功能:
请知道为什么会这样
我认为为 _id
传递的值是“未初始化的”,因此它的值是未定义的。如果没有看到更多您的代码,我无法判断。解决此问题的一种方法是在实例化员工时,确保 _id
实际上被设置为一个值,否则将其设置为 ''
(空字符串)并检查它是否具有值在你的 delete().
另一种选择是编辑您的删除功能
deleteEmployee(_id: string | undefined){
if(_id) { //Checks if the passed variable has a value
...
}
else {
// Error
}
}
我在 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',
组件模板出错EmployeeComponent
Employee
请知道为什么会这样
我认为为 _id
传递的值是“未初始化的”,因此它的值是未定义的。如果没有看到更多您的代码,我无法判断。解决此问题的一种方法是在实例化员工时,确保 _id
实际上被设置为一个值,否则将其设置为 ''
(空字符串)并检查它是否具有值在你的 delete().
另一种选择是编辑您的删除功能
deleteEmployee(_id: string | undefined){
if(_id) { //Checks if the passed variable has a value
...
}
else {
// Error
}
}