简体   繁体   English

如何更改计算属性vuejs中的数组元素

[英]How to change element of array in computed properties vuejs

i found similar problem on stackowerflow but still cant find solution我在 stackowerflow 上发现了类似的问题,但仍然找不到解决方案

Here is my problem,这是我的问题,

I have some data which i request from rest api, it is shipment from my e-commence site, this shipment contain array of items, and each item contain one product.我有一些我从 rest api 请求的数据,它是从我的电子商务网站发货,这批货物包含一系列项目,每个项目包含一个产品。 I created computed properti in my component products where i map items and create new array of product.我在我的组件products中创建了计算属性,我在其中映射了项目并创建了新的产品数组。 Issue is text, after i get my products from rest api, i need mark each product in this array as resolved, but it not works for me, looks like my new product array is not reactive问题是文本,在我从 rest api 获取我的产品后,我需要将此数组中的每个产品标记为已解决,但它对我不起作用,看起来我的新产品数组没有反应

Here is my code这是我的代码

Scan.vue扫描.vue

<script>
    import HeadLine from '@/components/HeadLine'
    import OrderDetails from "@/components/collect/OrderDetails"
    import ProductListItem from "@/components/collect/ProductListItem"
    import {mapActions} from 'vuex'

    export default {
        components: {
            HeadLine, OrderDetails, ProductListItem
        },
        data() {
            return {
                scan: '',
                shipment: {},
                loading: null,
                success: null,
            }
        },
        computed: {
            products() {
                return this.shipment.items ? this.shipment.items.map(item => {
                    return {
                        barcode: item.product.barcode,
                        name: item.product.name,
                        sku: item.product.sku,
                        success: null
                    }
                }) : []
            },
            orderDetails() {
                return {
                    tracking_number: this.shipment.tracking_number || '',
                    order_id: this.shipment.remote_order_id || ''
                }
            }
        },
        methods: {
            ...mapActions([
                'handleError',
                'getShipmentByTracking'
            ]),
            async handleGetShipmentByTracking() {
                try {
                    this.loading = true
                    this.shipment = await this.getShipmentByTracking(this.scan)
                    this.scan = ''
                } catch (e) {
                    this.handleError(e)
                    this.scan = ''
                } finally {
                    this.loading = null
                }
            },
            handleScan() {
                // If no shipment, get it by tracking number
                if (!this.shipment.id) {
                    return this.handleGetShipmentByTracking()
                }

                //We got shipment already, start scan products by barcode
                this.products.map((el) => {
                    if (el.barcode === this.scan) {
                        el.success = true
                    }
                })
            },
            clearOrder() {
                this.shipment = {}
                this.scan = ''
            }
        },
        watch: {
            products: {
                handler(val){
                    alert('changed')
                },
                deep: true
            }
        }
    }
</script>

<template>
    <v-row align="center" justify="center">
        <v-col cols="12" md="12" sm="12" xl="12">
            <head-line text="Scan tracking numbers"/>
        </v-col>
        <v-col cols="12" md="12" sm="12">
            <v-row>
                <v-col cols="12" md="6" sm="12">
                    <v-card>
                        <v-card-title>
                            Scan Area
                        </v-card-title>
                        <v-card-text>
                            <v-text-field
                                @keyup.enter="handleScan"
                                label="Scan"
                                clearable
                                loader-height="3"
                                :loading="loading"
                                v-model="scan"
                                outlined
                            ></v-text-field>
                            <v-btn
                                block
                                color="primary"
                                @click="clearOrder"
                            >
                                Clear Order
                            </v-btn>
                        </v-card-text>
                    </v-card>
                </v-col>
                <v-col cols="12" md="6" sm="12">
                    <order-details :details="orderDetails"/>
                </v-col>
            </v-row>
        </v-col>
        <v-col cols="12" sm="12" md="12" v-if="products">
            <product-list-item v-for="(product, index) in products" :key="index" :product="product" />
        </v-col>
    </v-row>
</template>

<style scoped>

</style>

ProductListItem产品列表项

<script>
    export default {
        props: {
            product: {
                type: Object,
                default: {}
            },
        },
        computed: {
            color() {
                return this.product.success ? 'green' : 'white'
            }
        }
    }
</script>

<template>
    <v-card class="m-1" :color="color">
        <v-card-title>
            {{product.name}}
        </v-card-title>
        <v-card-text class="title font-weight-black">
            <div>
                <v-icon class="mr-1">mdi-pound</v-icon>{{product.sku}}
            </div>
            <div>
                <v-icon class="mr-1">mdi-barcode</v-icon>{{product.barcode}}
            </div>
        </v-card-text>
    </v-card>
</template>

<style scoped>

</style>

And here is console.log of my this.products这是我的this.products console.log 在此处输入图片说明

I marked products as successfull but color in my ProductListItem component still white我将产品标记为成功,但我的ProductListItem组件中的颜色仍然是白色

Two suggestions:两个建议:

  1. Make a default value of the product prop in the ProductListItem as function returning object请在ProductListItem产品道具的默认值作为函数返回对象
  2. The default value of the product prop should contain success prop产品道具的默认值应包含成功道具

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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