Hello, I'm facing an issue with my Observable and need some guidance.
Currently, I have a functional cart and checkout system that works well when adding items to the cart. It successfully manages total items and costs. These components are located as siblings within the shopping-cart folder structure. However, I am experiencing an inconsistency where the total is only updated correctly after reloading the page. There is an image next to the shopping cart which fails to update the total number of items until a reload is performed.
Here is a visual representation: https://i.sstatic.net/f5yCi2S6.png
The problematic area lies in the header component which resides in a separate folder named UI in the hierarchy. Despite attempting various solutions, the issue persists. Understanding this will not only help me resolve the current problem but also assist in implementing other updates in the header component such as displaying user login information without requiring a page reload. Any advice on how to rectify this through subscription handling would be highly appreciated. Below is the code snippet for the header component:
header.component.ts
Component({
providers:[NgbModal, NgbModalConfig, ProductItemComponent, ProductListComponent,
CartItemComponent, EventEmitter, CartComponent, WishlistListComponent],
//NgbModal is a service for modal dialog
selector: 'app-header',
templateUrl: './header.component.html',
styleUrls: ['./header.component.scss']
})
constructor( private msg: MessengerService, private
cartService:CartService,private accountService: AccountService, private
modalService: NgbModal, private config: NgbModalConfig,
private searchService:SearchService, private auth:AuthService, public
prodcomp:ProductItemComponent,
private _imageService:ImageService, private formBuilder:
UntypedFormBuilder,private alertService: AlertService, private _
wishlistitemService: WishlistItemService)
{
//Attempted this approach without success
// this.cartService.itemTotals$.subscribe((v: any) => (this.itemTotal$ = v));
}
ngOnInit(): void {
this.loadCartItems();
}
loadCartItems(){
this.cartService.getCartItems().subscribe((item) => {
this.cartitems$ = item;
// alert("A cart item" + item);
this.calNumberOfItems();
})
}
calNumberOfItems(){;
for(this.qty=0; this.qty < this.cartitems$.length; this.qty++){
this.qty
}
this.itemTotal$ = this.qty;
}
header.html
</span><span ><i class="fas fa-circle" aria-hidden="true" style="font-size: 25px;
margin-top: -20px; cursor:none; color:#1D004F;"><strong style="color:#ffffff; margin-
left: -18px; padding-top: 0; font-size:10px; text-align: center; line-height: 20px;">
{{itemTotal$}}</strong></i></span></a>
cart.service.ts
export interface Cartitems{
_id: string,
name: string,
size: string,
qty: number,
price: number,
imageUrl:string
}
@Injectable({
providedIn: 'root'
})
export class CartService {
rootUrl = 'api';
cartitems$= new BehaviorSubject<Cartitems[]>([]); //this works
//itemTotals$ = new BehaviorSubject<Cartitems[]>([]);
getCartItems(): Observable<Cartitems[]> {
return this.http.get<Cartitems[]>(this.rootUrl + '/getProducts/cartitems')
}
}
Only the relevant parts of the code were shared. If anyone could provide insights into what might be missing or causing the issue, it would be greatly appreciated. Thank you in advance.