【Vue3】 The complete writing method of computed is to select all and invert the selection, and calculate the total price of the product.

Select all and invert selection

  const allCheck = computed({
            get() {
                return buyCard.value.every(item => item.checkState)
            },
            set(val) {
                return buyCard.value.forEach(item => item.checkState = val);
            },
        });

Calculate total product price

const aggregatePrice = computed(() => {
  const arr = buyCard.value.filter(item => item.checkState)
  return arr.reduce((sum, item) => sum + item.price * item.inputValue, 0)
})

Guess you like

Origin blog.csdn.net/m0_64494670/article/details/134454331