Vue JS这个。$ set not update reaction

时间:2018-06-11 15:41:53

标签: javascript arrays vue.js vuejs2

我有一个v-for渲染表,其中包含产品。此表有一个“活动”状态列,我希望用户能够单击活动按钮并使其变为非活动状态,或者单击非活动按钮并使其变为活动状态(基本上是切换开关)。

我通过对api路由进行POST调用来实现此功能,其中我的状态已更新。这很好用。

问题是我不能让vueJS多次更新数组中受影响的对象。这个。$ set工作一次。如果我第二次按下拨动开关,它就不再有效了。

这是我的表:

<table class="table table-striped fancy-table">
                                        <thead>
                                            <tr>
                                                <th class="text-center">&nbsp;</th>
                                                <th>Product/Service</th>
                                                <th class="text-center">Status</th>
                                                <th class="text-center">Date Added</th>
                                                <th class="text-center">QTY Sold</th>
                                                <th class="text-center">&nbsp;</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <tr v-for="(service, index) in services">
                                                <td class="text-center">
                                                    <img v-if="service.featured_image" :src="service.featured_image" class="table-thumb">
                                                    <img v-else src="/img/default-product.png" class="table-thumb">
                                                </td>
                                                <td>{{service.service_name}}<span class="secondary">${{parseFloat(service.price).toFixed(2)}}</span></td>
                                                <td class="text-center">
                                                    <a href="#" v-if="service.active === 1" @click.prevent="updateStatus(service, index, 0)"><i class="fas fa-circle active"></i></a>
                                                    <a href="#" v-else="service.active === 0" @click.prevent="updateStatus(service, index, 1)"><i class="fas fa-circle inactive"></i></a>
                                                </td>
                                                <td class="text-center">{{ service.created_at | moment("dddd, MMMM Do YYYY") }}</td>
                                                <td class="text-center">10</td>
                                                <td class="text-center"><a href="#"><i class="fal fa-edit table-action-btn"></i></a></td>
                                            </tr>
                                        </tbody>
                                    </table>

这是我控制更新的方法:

updateStatus: function(service, index, status) {

            // Grab the authorized user
            const authUser = JSON.parse(window.localStorage.getItem('authUser'))

            // Add a role and refresh the list
            this.$http.post('/api/vendor/services/update/' + service.id + '?updateActive=' + status, { }, { headers: { Authorization: 'Bearer ' + authUser.access_token } }).then((response) => {
                // update this service
                this.$set(this.services, index, response.body);
            }).catch(function(error){
                console.log(error);
            }) 

        }

编辑:

我在v-for表中添加了一个:key param,但我仍然遇到同样的问题。正如您在我的网络面板中看到的,第一次单击按钮时,它会按预期运行。使用updateActive = 0发布到api路由。第二次单击按钮时,它会使用updateActive = 1发布到api路由,并且数据在服务器端更改,但此时,我的services数组中的对象没有更新,所以它现在只是不断发布updateActive = 1而不是显示我的另一个按钮(切换开关,就像我想要的那样)。

这是我的网络面板的屏幕截图,只需点击4次按钮。

enter image description here

2 个答案:

答案 0 :(得分:0)

此处的问题可能是key

中缺少v-for

<tr v-for="(service, index) in services">

你可以使用索引中的键,但在某些情况下也可能会导致问题(例如,当删除数组中的单个项时,最后的DOM对象将被删除,因为键移位)< / p>

<tr v-for="(service, index) in services" :key="index">

理想情况下,您可以使用

等数据中的独特内容

<tr v-for="(service, index) in services" :key="service.id">

答案 1 :(得分:0)

感谢上面的@Daniel,答案是我期待一个数字值(因为它在MySQL中设置为整数)并且我的ajax调用将该字段作为字符串返回。