clearCart() {
console.log('clearCart method called'); // 打印一条日志来确认这个方法被成功调用
this.cartItems = [];
this.totalPrice.next(0);
this.totalQuantity.next(0);
}
clearCart() {
setTimeout(() => {
this.cartItems = [];
this.totalPrice.next(0);
this.totalQuantity.next(0);
}, 500); // 设置500毫秒的等待时间
}
注意: 如果这种方法解决了问题,最好使用ngZone来确保Angular界面的更新被检测并更新到UI上。
import { NgZone } from '@angular/core';
...
constructor(
private ngZone: NgZone
) { }
...
clearCart() {
setTimeout(() => {
this.cartItems = [];
this.totalPrice.next(0);
this.totalQuantity.next(0);
this.ngZone.run(() => {});
}, 500);
}
这将确保在更新后,Angular界面将正确显示更新的状态。