最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Maximum call stack size exceeded - Vue.js - Stack Overflow

programmeradmin2浏览0评论

I've got a puted method that allows me to count a total price of my products and discount value and would like to get the value of: total - discount.

cartTotal() {
    var total = 0;
    var discount = Math.round((0.1 * this.cartTotal) * 100) / 100;
    this.cart.items.forEach(function(item) {
      total += item.quantity * item.product.price;
    });
    total -= discount;
    return total;
}

Doens't work for me and I get that Maximum call stack size exceeded error.

I've got a puted method that allows me to count a total price of my products and discount value and would like to get the value of: total - discount.

cartTotal() {
    var total = 0;
    var discount = Math.round((0.1 * this.cartTotal) * 100) / 100;
    this.cart.items.forEach(function(item) {
      total += item.quantity * item.product.price;
    });
    total -= discount;
    return total;
}

Doens't work for me and I get that Maximum call stack size exceeded error.

Share Improve this question edited Feb 28, 2018 at 21:29 kabugh asked Feb 28, 2018 at 20:43 kabughkabugh 3151 gold badge9 silver badges32 bronze badges 10
  • Are these puted variables? – Bharathvaj Ganesan Commented Feb 28, 2018 at 20:47
  • You expect this.discountValue.discount to call your discountValue() method? – Crescent Fresh Commented Feb 28, 2018 at 20:47
  • 1 discountValue.discount makes no sense (neither does setting discountActive = true then immediately throwing it away). What are you trying to do? – Dave Newton Commented Feb 28, 2018 at 20:47
  • 2 discountValue() seems to assume that cartTotal() returns a price without the discount subtracted from it, but then in cartTotal() you want to subtract the discount from it. There is a logical error here. You must make up your mind: does cartTotal() include the discount or not? – trincot Commented Feb 28, 2018 at 20:52
  • I've got some products that have prices and after typing a correct code to give discount to the price of that product. So I calculate the value of the discount and would like it to be 0,1 of my total price: ` var discount = Math.round((0.1 * this.cartTotal) * 100) / 100` - that's correct. Then would like to subtract that value from my main value total. – kabugh Commented Feb 28, 2018 at 20:54
 |  Show 5 more ments

1 Answer 1

Reset to default 5

You're getting that error because you have two puted properties that reference each others' value. Whenever you do that, you create a cyclical dependency which will generate a "Maximum call stack size exceeded" error.

You really have three values you're dealing with 1) a sum of all values in the cart, 2) a discount amount, and 3) a total value which is the sum minus the discount.

You should have three puted properties:

puted: {
  cartSum() {
    return this.cart.items.reduce((n, i) => n += i.quantity * i.product.price, 0);
  },
  discountValue() {
    return Math.round((0.1 * this.cartSum) * 100) / 100;
  },
  cartTotal() {
    return this.cartSum - this.discountValue;
  },
}
发布评论

评论列表(0)

  1. 暂无评论