【问题标题】:Span not changing value after clicking the increment button (Vue.js)单击增量按钮后跨度不更改值(Vue.js)
【发布时间】:2020-10-10 09:26:05
【问题描述】:

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

代码如下:

App.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

【问题讨论】:

    标签: vue.js vuejs2 vue-directives vuejs3


    【解决方案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;
        })
    }
    

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

    &lt;button class="button test" @click="decrement(p.id)"&gt;-&lt;/button&gt;

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

    【讨论】:

    • 没问题@TechDev ?
    【解决方案2】:

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

            <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

    【讨论】:

      猜你喜欢
      • 2019-01-01
      • 2022-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-09
      • 2012-06-14
      • 1970-01-01
      • 2019-03-28
      相关资源
      最近更新 更多