1

我是 Vue.js 的新手。当我单击增量按钮时,跨度值根本不会改变。我已经添加了点击方法并添加了一个条件。但它仍然没有改变跨度。谁能帮我?

这是代码:

应用程序.vue

<template>
  <div id="app">
    <table class="table">
      <tbody>
        <tr class="tr" v-for="(p,index) in cart" :key="index">
          <td>
            <div class="columns is-multiline is-mobile is-hidden-desktop multilines">
              <div class="column is-half">
                <div class="buttons has-addons">
                  <button class="button test">-</button>
                  <span class="button test" id="value-quantity">{{p.quantity}}</span>
                  <button class="button test" id="increment-number" @click="increment">+</button>
                </div>
              </div>
            </div>
          </td>
        </tr>
        <!-- {{this.cart}} -->
      </tbody>
    </table>
  </div>
</template>

<script>
import HelloWorld from "./components/HelloWorld";

export default {
  name: "App",
  components: {
    HelloWorld
  },
  methods: {
    increment() {
      this.quantity++;
      // this.price = this.quantityorder * 8000
      // alert(this.price)
    }
  },
  data() {
    return {
      cart: [
        {
          id: 4,
          quantity: 1
        },
        {
          id: 1,
          quantity: 3
        },
        {
          id: 2,
          quantity: 3
        },
        {
          id: 3,
          quantity: 1
        },
        {
          id: 7,
          quantity: 2
        }
      ]
    };
  }
};
</script>

<style>
#app {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

可以在这里运行:

https://codesandbox.io/s/gifted-hooks-3po0z?file=/src/App.vue

4

2 回答 2

1

您必须更改购物车变量中的商品数量。现在您正在更改其他数据。

        <tr class="tr" v-for="(p) in cart" :key="p.id">
          <td>
            <div class="columns is-multiline is-mobile is-hidden-desktop multilines">
              <div class="column is-half">
                <div class="buttons has-addons">
                  <button class="button test">-</button>
                  <span class="button test" id="value-quantity">{{p.quantity}}</span>
                  <button class="button test" id="increment-number" @click="increment(p.id)">+</button>
                </div>
              </div>
            </div>
          </td>
        </tr>

和方法:

    increment(id) {
      this.cart.forEach((item, i) => {
        if (item.id === id) {
          this.cart[i].quantity += 1;
        }
      });
    }

你也在这里检查:https ://codesandbox.io/s/mutable-star-mq6o6

于 2020-06-20T05:09:49.317 回答
1

在您的increment方法中,您不是指任何特定数量,您需要做的是将产品 ID 传递给它,即@click="increment(p.id)",然后遍历您的购物车数据属性并使用 if 语句查看 ID 是否匹配,然后增加该特定项目的数量:

increment(id) {
    this.cart.forEach((item, i) => {
        if (i.id === id) this.cart[i].quantity += 1;
    })
}

如果你想添加一个减量方法,你几乎可以做同样的事情:

<button class="button test" @click="decrement(p.id)">-</button>

decrement(id) {
    this.cart.forEach((item, i) => {
        if (i.id === id) this.cart[i].quantity -= 1;
    })
}
于 2020-06-20T05:12:30.660 回答