简体   繁体   English

显示编号导航栏上购物车中的商品在刷新时显示为 0

[英]Display of no. of items in cart on navbar shows 0 on refresh

So in my angular application.所以在我的角度应用程序中。 I am trying to add movies to a cart.我正在尝试将电影添加到购物车。 I am able to add them to the cart, and the correct no.我可以将它们添加到购物车中,并且正确无误。 of movies in the cart are reflected back on the navbar, but on refresh, it turns to 0.购物车中的电影数反映在导航栏上,但在刷新时变为 0。

HTML of displaying count显示计数的 HTML

 <li *ngIf="signedIn" class="nav-item" routerLinkActive="active"> <a class="nav-link" [routerLink]="['/cart',authService.decodedToken.nameid]">Cart <span class="badge badge-dark">{{cartItemCount}}</span></a> </li>

My shared service through which I am updating-我正在更新的共享服务-

 export class SharedcartService { private currentCartCount = new BehaviorSubject(0); currentMessage = this.currentCartCount.asObservable(); constructor() { } addMovieToCart(movie: any) { sessionStorage.setItem('movie', JSON.stringify(movie)); } getMovieFromCart() { return JSON.parse(sessionStorage.getItem('movie')); } removeAllMovieFromCart() { return sessionStorage.removeItem('movie'); } updateCartCount(count: number) { this.currentCartCount.next(count); } }

My cart.ts file我的cart.ts文件

 export class CartComponent implements OnInit { user: User; defaultQuantity: number = 1; movieAddedTocart: any; allTotal: number; constructor(private http:HttpClient, private userService: UserService,private sharedService: SharedcartService, private alertify: AlertifyService,private route: ActivatedRoute ) { } ngOnInit() { this.route.data.subscribe(data=>{ this.user=data['user']; }); this.movieAddedTocart=this.sharedService.getMovieFromCart(); for (let i in this.movieAddedTocart) { this.movieAddedTocart[i].Quantity = 1; } this.sharedService.removeAllMovieFromCart(); this.sharedService.addMovieToCart(this.movieAddedTocart); this.calculteAllTotal(this.movieAddedTocart); } onRemoveQuantity(movie: any) { this.movieAddedTocart=this.sharedService.getMovieFromCart(); this.movieAddedTocart.find(p=>p.aMovieId == movie.aMovieId).Quantity = movie.Quantity-1; this.sharedService.removeAllMovieFromCart(); this.sharedService.addMovieToCart(this.movieAddedTocart); this.calculteAllTotal(this.movieAddedTocart); } calculteAllTotal(allItems:any) { let total = 0; for (let i in allItems) { total= total+(allItems[i].Quantity *allItems[i].aPrice); } this.allTotal=total; } }

刷新时的导航栏

可能是因为 ngOnInit 它正在执行this.sharedService.removeAllMovieFromCart()从购物车中删除所有电影

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM