我怎样才能使用增加的相同数字来降低价格?
How can I decrease the price using the same number used to increase?
我试图在用户使用初始价格按下减号图标时降低价格,但增加后的新价格是初始价格
quantityHandler: function (action, product) {
// Increase or decrease quantity of product
try {
const newItems = [...this.products]; // clone the array
let idx = newItems.indexOf(product);
let currentQty = newItems[idx].productQuantity;
let price = newItems[idx].unitPrice;
if (action == "more") {
newItems[idx].productQuantity = currentQty + 1;
newItems[idx].unitPrice = price + price;
} else if (action == "less") {
newItems[idx].productQuantity = currentQty > 1 ? currentQty - 1 : 1;
// Decrease current price using the initial price
}
this.products = newItems; // set new state
console.log(this.products);
} catch (error) {
alert(error);
}
},
您可以使用负数。类似于:
newPrice = currentQty * price
newPactionrice = 'less'? newPrice = newPrice * -1
// then add that price... + a negative number will decrease the total
quantityHandler: function (action, product) {
// Increase or decrease quantity of product
try {
const newItems = [...this.products]; // clone the array
let idx = newItems.findIndex((item) => { //compare with some unique identifier
return item.id === product.id
})
let currentQty = newItems[idx].productQuantity;
let price = newItems[idx].unitPrice;
let perQtyPrice = price / currentQty //per unit price
if (action == "more") {
newItems[idx].unitPrice += perQtyPrice;
newItems[idx].productQuantity = currentQty + 1;
} else if (action == "less") {
if(currentQty > 1) {
newItems[idx].unitPrice -= perQtyPrice;
newItems[idx].productQuantity = currentQty > 1 ? currentQty - 1 : 1;
}
}
this.products = newItems; // set new state
console.log(this.products);
} catch (error) {
alert(error);
}
},
您需要找到每件商品的价格,然后用它来减去或加上价格
我试图在用户使用初始价格按下减号图标时降低价格,但增加后的新价格是初始价格
quantityHandler: function (action, product) {
// Increase or decrease quantity of product
try {
const newItems = [...this.products]; // clone the array
let idx = newItems.indexOf(product);
let currentQty = newItems[idx].productQuantity;
let price = newItems[idx].unitPrice;
if (action == "more") {
newItems[idx].productQuantity = currentQty + 1;
newItems[idx].unitPrice = price + price;
} else if (action == "less") {
newItems[idx].productQuantity = currentQty > 1 ? currentQty - 1 : 1;
// Decrease current price using the initial price
}
this.products = newItems; // set new state
console.log(this.products);
} catch (error) {
alert(error);
}
},
您可以使用负数。类似于:
newPrice = currentQty * price
newPactionrice = 'less'? newPrice = newPrice * -1
// then add that price... + a negative number will decrease the total
quantityHandler: function (action, product) {
// Increase or decrease quantity of product
try {
const newItems = [...this.products]; // clone the array
let idx = newItems.findIndex((item) => { //compare with some unique identifier
return item.id === product.id
})
let currentQty = newItems[idx].productQuantity;
let price = newItems[idx].unitPrice;
let perQtyPrice = price / currentQty //per unit price
if (action == "more") {
newItems[idx].unitPrice += perQtyPrice;
newItems[idx].productQuantity = currentQty + 1;
} else if (action == "less") {
if(currentQty > 1) {
newItems[idx].unitPrice -= perQtyPrice;
newItems[idx].productQuantity = currentQty > 1 ? currentQty - 1 : 1;
}
}
this.products = newItems; // set new state
console.log(this.products);
} catch (error) {
alert(error);
}
},
您需要找到每件商品的价格,然后用它来减去或加上价格