Vue购物车产品的变化会添加到购物车中,但购物车中的商品会发生变化,而不是添加新商品

时间:2020-04-02 19:31:48

标签: vue.js vuex back-button product-variations

我有一个带有vuex的vue版本“ ^ 2.6.11”应用,在其中我将商品添加到购物车中。 我的商品有颜色之类的变化,因此在添加到购物车中之前,我要先比较两个条件,以比较颜色和产品ID。

product.productId == item.productId && product.colour == item.colour

从选择框中选择颜色,使用选择框中的更改事件中的选定颜色设置产品的属性。

this.item.colour = event.target.value

问题是,即使例如更改颜色,它也会更新购物车中的当前商品:

  • 选择框显示金色,单击“添加到购物车”,并将一个金色项目添加到购物车。

  • 选择框更改为银色,产品组件的颜色属性更改为银色,然后单击“添加到购物车”。

  • 购物车数量中的当前项目更改为2并且其颜色 变成银色。

奇怪的是,当我导航到结帐然后单击“后退”按钮时,“添加到购物车”通过将商品的新变体添加到购物车中来实现我想要的方式,但是,当我在就像第一次一样。

我希望这是有道理的。 我不明白为什么在单击后退按钮之前它不起作用。

我无法设置Jsfiddle或代码段,因为这里有很多组件一起工作,所以我将在下面添加一些代码。

// Product.vue

<template>
  <div>
    <b-col>
      <b-card
        v-bind:title="product_type"
        v-bind:img-src="imagePath"
        img-alt="Image"
        img-top
        tag="article"
        style="max-width: 15rem;"
        class="mb-2">
        <b-card-text>{{ product_type }}</b-card-text>
        <div class="form-group">
          <label for="colourSelect">Please select a colour</label>
          <select @change="onChange($event)">
            <option v-for="item in metal_colour" v-bind:key="item">
              {{ item }}
            </option>
          </select>
        </div>
        <div class="clearfix">
          <div class="price pull-left" v-bind:key="price">€{{ price }}</div>
          <div class="text-right">
            <b-button @click="addToCart" variant="primary">Add to cart</b-button>
...
</template>
<script>
export default {
  name: 'Product',
  props: {
    productId: Number,
    imagePath: String,
    product_type: String,
    metal_colour: Array,
    price: Number,
    qty: Number
  },
  data() {
    return {
      item: {
        productId: this.productId,
        imagePath: this.imagePath,
        product_type: this.product_type,
        price: this.price,
        colour: 'Gold',
        qty: 1
      }
    }
  },
  methods: {
    addToCart() {
      this.$store.commit('addToCart', this.item)
      this.$bvModal.show('modal-1')
    },
    onChange(event) {
      this.item.colour = event.target.value
    }
  }
}
</script>
// store/index.js Vuex

...
 mutations: {
    addToCart(state, item) {
      let found = state.inCart.find(
        product =>
          product.productId == item.productId && product.colour == item.colour
      )
      if (found) {
        found.qty++
      } else {
        state.inCart.push(item)
      }
    }
  },
...

对于我的购物车,我使用的是模态,其内容如下:

// Cart.vue
<div>
  <b-table striped :items="this.$store.state.inCart">

    <template v-slot:cell(imagePath)="data">
      <img :src="data.value" class="thumb" alt="product" />
      {{ data.imagePath }}
     </template>

  </b-table>
</div>

<div slot="modal-footer">

  <router-link to="/checkout">
    <b-button variant="primary" @click="$bvModal.hide('modal-1')">
      Checkout
    </b-button>
  </router-link>

  <b-button class="modalBtn" variant="primary" @click="$bvModal.hide('modal-1')">
    Continue Shopping
  </b-button>

</div>

1 个答案:

答案 0 :(得分:1)

您在this标记中使用了this.$store.state.inCart> <template>,应该只是$store.state.inCart,但这仍然应该标记一个错误,并且永远无法实际工作。

<b-table striped :items="$store.state.inCart">

您可能遇到反应性问题,请尝试以下方法:

import {mapState} from 'vuex'

computed: {

  ...mapState(['inCart'])

}

<b-table striped :items="inCart">


最后,当您将商品添加到购物车时,您需要添加item对象的COPY,因为您通过引用传递对象,并且由于进行state.inCart.push(item)而导致意外行为

有两种选择:

1)导入lodash> import { copy } from 'lodash'并使用copy(item)

2)JSON.parse(JSON.stringify(item))看起来很脏,但速度很快。

这两种方法将取消对对象的引用。

为澄清起见,您可以在vuex addToCart方法中执行以下复制过程:

addToCart(state, item) {

  let found = state.inCart.find(
    product => product.productId === item.productId && product.colour === item.colour
  )

  if (found) {

    found.qty++

  } else {

    // Clone the object.
    const insert = JSON.parse(JSON.stringify(item))
    state.inCart.push(insert)

  }
}