angular - Angular 9,按ID的http删除方法不起作用
问题描述
我正在尝试使用 delete 方法从虚假 API 服务中删除数据
这是我的代码component.html
<ul class="list-group">
<li *ngFor="let post of posts; let i = index" class="list-group-item">
<button class="btn btn-danger btn-sm" (click)="deletePost(post)">Delete</button>
{{i+1}} {{post.title}}
</li>
</ul>
和component.ts
// deletePost(post){
// this.service.deletePost(4000)
// .subscribe((response: any) => {
// console.log("inside Response")
// console.log(response.status)
// let index = this.posts.indexOf(post);
// this.posts.splice(index, 1);
// }, (error) => {
// console.log("inside error")
// if(error.status === 404){
// alert('this post has already deleted.')
// }
// else{
// alert('An error');
// console.log(error)
// }
// })
// }
deletePost(post){
this.service.deletePost(post.id)
.subscribe(
(response) => {
let index = this.posts.indexOf(post);
this.posts.splice(index, 1);
}, (error) => {
console.log(error)
})
}
服务.ts
private url = 'https://jsonplaceholder.typicode.com/posts';
deletePost(id): Observable<any>{
console.log(this.url + '/' + id);
return this.http.delete(this.url + '/' + id)
.pipe(catchError(this.handleError))
}
handleError(error){
return throwError(error.message || "An Error");
}
post.id
当我尝试使用像 101 或更多这样的静态数字而不是它没有引发错误并在网络选项卡中返回 200 状态代码并仍然删除元素时,假 API 上的帖子总数为 100页面虽然不是 id
解决方案
推荐阅读
- javascript - React Native Expo - 卡在“下载 javascript 包”
- javascript - 如何添加选项以使用 JavaScript 日历(在我的 Angular APP 中)将视图类型更改为天?
- jquery - 单击页面加载后添加的按钮
- python-3.x - 搜索文本文件以查找连续重复并返回其中数量最多的文本的最佳方法是什么?
- django - GeoDjango MultiPolygonField 是否应该接受多边形几何?
- html - 并排设置 2 Div 和 3rd Div 应低于 2nd Div
- python - 你如何获得一个 tkinter 规模有十进制值并锁定到一个基值
- python - 如何构建一个数据框来比较回归模型的预测结果和实际结果
- node.js - 如何将 webpack 块的名称及其哈希提取到外部文件,如 manifest.json
- javascript - 为列表创建了删除功能,但并非所有选中的项目都在删除