【问题标题】:Unable to calculate Total in my shopping-cart无法计算我的购物车中的总计
【发布时间】:2020-12-27 16:57:27
【问题描述】:

我创建了一个包含 2 个组件(产品列表和购物车列表)的购物车。当我单击产品列表中的“添加到购物车”按钮时,它已成功移入服务文件并从服务文件移至“购物车列表”,但 Total 显示未定义。请帮我解决这个购物车 Total 问题。

Product-list.ts 文件


    import { Component, OnInit } from '@angular/core';
    import { ProductService } from '../../../service/product.service';
    import { CartService } from '../../../service/cart.service';
    
    @Component({
      selector: 'app-product-list',
      templateUrl: './product-list.component.html',
      styleUrls: ['./product-list.component.css']
    })
    export class ProductListComponent implements OnInit {
    
      Productlist = [];
    
    
      constructor(private productservice: ProductService,
                  private cartservice: CartService) {
    
        this.Productlist = this.productservice.send()
       }
    
      ngOnInit(): void {
      }
    
      saveproduct(item) {
    
        this.cartservice.get(item)
    
      }
    }

    

购物车服务文件

    import { Injectable } from '@angular/core';
    
    @Injectable({
      providedIn: 'root'
    })
    export class CartService {
    
      cartproducts = [{Name: 'Mobile', Price: 13000, Qty: 1}]
    
      constructor() { }
    
    
      send() {
    
        return this.cartproducts
      }
    
       get(items) {
    
        this.cartproducts.push(items)
    
        console.log(this.cartproducts)
       }
    }
    

购物车列表文件

    
    import { Component, OnInit, ɵConsole, ɵɵNgOnChangesFeature } from '@angular/core';
    import { CartService } from '../../../service/cart.service';
    
    @Component({
      selector: 'app-cart',
      templateUrl: './cart.component.html',
      styleUrls: ['./cart.component.css']
    })
    export class CartComponent implements OnInit {
    
      cartlist = [];
    
      Total: number;
    
      constructor( private cartservice: CartService) {
    
        this.cartlist = this.cartservice.send()
    
        console.log(this.cartlist);
    
        console.log(this.Total);
        
       }
    
      ngOnInit() {
    
        this.cartlist.forEach((_e: any) => {
    
          this.Total += (_e.qty * _e.price);
      
        });
      }
    }

【问题讨论】:

  • 听起来您可能需要学习如何使用调试器来单步调试您的代码。使用好的调试器,您可以逐行执行您的程序,并查看它与您期望的偏差在哪里。如果您要进行任何编程,这是必不可少的工具。延伸阅读:ericlippert.com/2014/03/05/how-to-debug-small-programs

标签: javascript angular


【解决方案1】:

创建Total 属性后,默认为undefined,直到您为其分配值。您确实在ngOnInit 中为其分配了一个值,但您计算的是undefined 值,因此它也返回undefined。给Total 一个默认值,如下所示:

    ...

    Total: number = 0;

    ...

此外,您在ngOnInit 中输入用于计算Total 的代码最好放在get 属性中。这样,Total 将始终在请求时计算,而不是预渲染,并且只计算一次。在这种情况下,您的代码将如下所示:

    get Total(): number {
      let value = 0;
      this.cartlist?.forEach((_e: any) => {
          value += (_e.qty * _e.price);
        });
      return value;
    }

【讨论】:

  • 我试过了,但问题没有解决.....仍然显示未定义。
  • 如果你指定一个默认值 0,那么它至少应该返回 0。您可以在此处更新帖子中的代码或在 Stackblitz 中设置测试环境吗?
  • 可能是因为ng serve 重新启动。如果您的代码似乎无法编译,这有时会有所帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多