我目前正在练习 Angular 并从它开始,我也在做一个 MEAN 堆栈应用程序,并且我正在尝试创建一个删除函数以便从数据库中删除一个元素,但它不起作用,我不断得到这个错误:
我还设置了
"noPropertyAccessFromIndexSignature": false,
,因为我读到我当前版本的角度需要禁用该选项才能使用我的代码。
我正在尝试从数据库中删除一个元素,元素的读取有效,但我无法删除它们。这是我的代码:
export class ListarProductoComponent implements OnInit {
listProductos: Producto[] =[];
constructor(private _productoService: ProductoService,
private toastR: ToastrService){}
ngOnInit(): void {
this.obtenerProductos();
//throw new Error('Method not implemented.');
}
obtenerProductos(){
this._productoService.getProductos().subscribe(data=>{
//console.log(data);
this.listProductos = data;
}, error=>{
console.log(error)
})
}
eliminarProducto(id: any) {
this._productoService['eliminarProducto'](id).subscribe((res: any) => {
console.log("product deleted")
this.obtenerProductos();
}, (error: any) => {
console.log(error);
});
}
}
我从我的 html 文件中调用:
<i (click)="eliminarProducto(producto._id)" class="fa-solid fa-trash text-danger"></i>
所以我想知道我可以使用什么解决方案,谢谢。
您的
eliminarProducto
中没有声明名为 ProductoService
的方法,请重新检查 ProductoService
您是否在其上声明了函数,如下所示!
import { of } from 'rxjs/operators';
...
export class ProductoService {
...
eliminarProducto(id: any) {
// write your code here ignore the below line
return of(true);
}
...
}