【问题标题】:In Vue.js app; how to add 'active' class to button in v-for loop在 Vue.js 应用程序中;如何在 v-for 循环中将“活动”类添加到按钮
【发布时间】:2018-03-30 17:54:41
【问题描述】:

在我的 Vue 应用程序中:我使用 v-for 来呈现产品 div,每个 div 都有一对按钮,“新”和“使用”,我想在按钮时添加“活动”类点击。问题是我正在向所有“新”按钮或所有“使用”按钮添加“活动”类;很确定有一种 Vue 方法可以做到这一点,除了获取单击按钮的索引并将类设置为相应的 el。我错过了什么?

标记:

<div id="app">
    <div class="products">

        <div class="product" v-for="product in products" :key="product.productID">
            <div class="product-image">
                <img :src=product.imgDef :alt="product.name" v-bind:title="product.name" />
            </div>
            <div class="product-summary">
                <p>{{ product.name }}</p>
                <p>{{ product.info }}</p>
                <ul>
                    <li v-for="detail in product.details">{{ detail }}</li>
                </ul>

                <div
                    class="conditions"
                    v-for="variant in product.variants"
                    v-bind:key="variant.variantID"
                    v-bind:style="variant.variantStyleObj"
                >
                    <!--
                        renders a 'new' button and a 'used' button in each div.product;
                        - clicking should add 'active' class only to clicked button
                    -->
                    <button
                        type="button"
                        v-on:click="updateProductImage(variant.variantImage, variant.variantID, product.productID, variant.condition)"
                        :class="[ 'condition', variant.condition, {active: conditionClass == variant.condition} ]"
                    >
                    {{ variant.condition }}
                    </button>
                </div>
            </div><!-- END .product-summary -->
        </div><!-- END .product -->
</div><!-- END #app -->

js:

var app = new Vue({
    el: '#app',
    data: {
        products: [
            {
                productID: 1,
                name: "",
                info: "",
                imgDef: "../../images-dev/...jpg",
                details: [],
                inStock: false,
                variants: [
                    {
                        variantID: "123",
                        variantImage: "../../images-dev/1a.jpg",
                        condition: "new"
                    },
                    {
                        variantID: "321",
                        variantImage: "../../images-dev/1b.jpg",
                        condition: "used"
                    }
                ],
                inventory: 9
            },
        cart: 0,
        // conditionClass: ""
    },
    methods: {
        addToCart: function() {
            this.cart += 3;
        },
        updateProductImage: function(variantImage, variantKey, productKey, variantCondition) {
            this.products[productKey-1].imgDef = variantImage;
            this.conditionClass = variantCondition;
        }
    }
})

【问题讨论】:

  • 为每个变体创建一个条件类并更改它而不是全局条件类
  • 谢谢@JulioGuerra。如果你不能触摸数据结构,你会如何解决这个问题?
  • 可以同时激活多个按钮吗?
  • 不,视图中只有一个活动按钮

标签: vue.js


【解决方案1】:

如果您不想更改 products 数组,可以,而不是使用单个全局 conditionClass

var app = new Vue({
    el: '#app',
    data: {
        // ...
        cart: 0,
        conditionClass: ""
    },

将其制成一个对象,以某种方式将其作为一个地图工作,该地图将以productID 作为键,产品的conditionClass

Demo JSFiddle here.

例子:

var app = new Vue({
    el: '#app',
    data: {
        // ...
        cart: 0,
        conditionClass: {} // initialize as empty object
    },

:class 更改为通过密钥访问conditionClass(为简洁起见,去掉了其他部分):

来自

<button ... :class="[{active: conditionClass == variant.condition}]">

收件人:

<button ... :class="[{active: conditionClass[product.productID] == variant.condition}]">

最后,在方法中设置键:

updateProductImage: function(variantImage, variantKey, productKey, variantCondition) {
  this.products[productKey - 1].imgDef = variantImage;
  // line below was: this.conditionClass = variantCondition;
  Vue.set(this.conditionClass, productKey, variantCondition);
}

【讨论】:

  • 谢谢@acdcjunior。我在您的 Fiddle 中注意到您的解决方案允许“新”或“二手”保持活动状态每个产品;虽然我的意图是在视图中只有一个活动按钮,但您的版本可能是一个有用的功能。我很快就会去实施你的方法,干杯。
  • 啊,如果您希望整个应用程序只有一个活动,请改用该方法的最后一行:Vue.set(this, 'conditionClass', {[productKey]: variantCondition});
  • 再次感谢 @acdcjunior,感谢您的解决方案并带领我阅读 Vue.set
猜你喜欢
  • 1970-01-01
  • 2021-04-11
  • 1970-01-01
  • 1970-01-01
  • 2017-10-12
  • 1970-01-01
  • 2022-07-29
  • 1970-01-01
  • 2019-02-04
相关资源
最近更新 更多