【发布时间】:2020-11-08 06:59:09
【问题描述】:
我只是想在点击时删除一个项目,我做了一个代码但我有错误,我已经卡了 2 天了。
ERROR TypeError: this. addedBook.indexOf 不是函数
我已经在网站上问过这个问题,我们因缺乏信息而关闭了它,但我很清楚和准确
感谢您的帮助
服务
export class BookService {
url: string = 'http://henri-potier.xebia.fr/books';
public booktype: BookType[];
item: any = [];
constructor(private http: HttpClient) { }
getBookList(): Observable<BookType[]> {
return this.http.get<BookType[]>(this.url);
}
addToBook() {
this.item.push(this.booktype);
}
}
addToBook() 在这里添加书籍,但我不知道如何使用它在我的 ts 文件中显示添加的书籍
ts.file
export class PaymentComponent implements OnInit {
addedBook: any = [];
product:any;
constructor(private bookService: BookService) { }
ngOnInit(): void {
this.addedBook = this.bookService.getBookList();
}
delete() {
this.addedBook.splice(this.addedBook.indexOf(this.product), 1);
}
}
html
<div class="product" *ngFor="let book of addedBook | async">
<div class="product-image">
<img [src]="book.cover" alt="book">
</div>
<div class="product-details">
<div class="product-title">{{book.title}}</div>
</div>
<div class="product-price">{{book.price | currency: 'EUR'}}</div>
<div class="product-quantity">
<input type="number" value="1" min="1">
</div>
<div class="product-removal">
<button class="remove-product" (click)="delete()">
Supprimé
</button>
</div>
界面
export interface BookType {
title: string;
price: number;
cover: string;
synopsis: string;
}
【问题讨论】:
-
this.bookService.getBookList() 返回什么?
-
@amerenderSingh 显示来自 api 的数据
-
您正在使用异步管道来获取添加的书籍,这可能意味着添加的书籍不是数组而是数组的可观察对象(即使看起来它正在初始化为组件?),所以这可能就是您收到错误的原因。你能添加服务的代码吗?这会给它带来一些光明。
标签: javascript angular typescript