简体   繁体   中英

Vue.js some items in card are not reactivity

I'm struggling with this problem for days now. I hope someone can help me out.

I have a checkout card where I'm trying to update the quantity and total price. :

The parent component :

<template>
    <div>
        <upsell-checkout-product
                :product="product"
                :index="index"
                @remote="remove"
                @increment="increment"
                @decrement="decrement"
        ></upsell-checkout-product>
    </div>
</template>

<script>
    export default {
        props: {
            runId: {
                type: String,
                default: null
            },
            card: {
                type: Array,
                default: null
            },

            customer: {
                type: Object,
                default: null
            },

            order: {
                type: Object,
                default: null
            }
        },

        data() {
            return {
                localCard: []
            }
        },

        mounted() {
            this.localCard = this.card;
        },

        methods: {
            increment(index) {
                Vue.set(this.localCard[index], 'quantity', this.localCard[index].quantity+=1);
            },

            decrement(index) {
                if(this.localCard[index].quantity > 0) {
                    this.localCard[index].quantity--;
                }
            },

            remove(index) {
                this.localCard.splice(index, 1);
            }
        }
    }
</script>

Child component:

   <template>
        <div class="text-green">
            <div class="flex center p-2">
                <div class="w-1/3">
                    <img class="rounded-full h-24 w-24 border border-green"
                         :src="'/storage/articles/' + product.article.image_url"
                         v-if="product.article.image_url"
                    />
                </div>

                <div class="w-2/3">
                    <h3>{{ product.article.name }}</h3>

                    <h5 class="mt-2">Bestaalstatus</h5>

                    <div>
                        <div class="bg-red text-white text-xs mt-2 rounded p-1 w-1/3">
                            Niet voldaan
                        </div>
                    </div>
                </div>
            </div>

            <div class="center justify-between p-2 border-t border-b border-dotted border-green">
                <div class="w-1/5">
                    <div class="cursor-pointer" @click="$emit('remove', index)">
                        x
                    </div>
                </div>

                <div class="center w-2/5">
                    <div class="cursor-pointer" @click="$emit('decrement', index)">
                        -
                    </div>

                    <input type="number"
                           class="input-main w-1/2 ml-1 mr-1 h-8"
                           v-model="product.quantity">

                    <div class="cursor-pointer" @click="$emit('increment', index)">
                        +
                    </div>
                </div>

                <div class="flex justify-end w-2/5">
                    <div v-if="product.article.member_discount_percentage">
                        <p class="text-sm">€ <strike>{{ price }}</strike> - € {{ discountPrice }}</p>
                    </div>

                    <div v-else>
                        <p class="text-sm">€ {{ discountPrice }}</p>
                    </div>
                </div>
            </div>
        </div>
    </template>

    <script>
        export default {
            props: ['product', 'index'],

            computed: {
                price: function() {
                    return (this.product.quantity * this.product.article.price).toFixed(2);
                },

                discountPrice: function() {
                    return (this.product.quantity * this.product.article.discount_price).toFixed(2);
                }
            }
        }
    </script>

The problem is that card item's I already fetched from the server are reactive in the localCard, but item's that have not been fetched from the server but added throughout the checkout process are not...

I already tried to make a JsFiddle but I can't reproduce the problem. Obviously it's a reactivity problem, but I have no idea how I should fix this.

在此处输入图片说明

I need some help here, thank you!

Finally I have fixed it!

When I push new items to the array and do this:

.push(Object.assign({}, article));

It suddenly works! Thanks for helping.

I think the problem is not in the code you showed us.

mounted() {
            this.localCard = this.card;
        },

I think that this.card is not reactive (at least not for all items) and that is making you trouble. You could check my assumption by a simple console.log(this.card) and there you can see wether all items in the cards-Array are Observers.

Hopefully this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM