数组内对象的VueJS反应性问题

Cri*_*on7 2 vue.js vue-component vuejs2 vuetify.js

我已经声明了我的数据对象,queuedItemList: []它应该包含由后台服务提供的一组项目。在 HTTP 请求之后,我使用类似的 for 循环填充列表

for(var i = 0; i < getList.length; i++) {
    var newObject = Object.assign({}, getList[i]);
    this.queuedItemList.splice(i, 1, newObject);
}
Run Code Online (Sandbox Code Playgroud)

用于填充以下模板

<Item v-for="(item, index) in queuedItemList" :key="index" :componentData="item" :index="index" @remove="removeItem">
</Item>
Run Code Online (Sandbox Code Playgroud)

I am supposed to do a periodic HTTP GET to get the new list of items, which may also contain current items with maybe different state. Inside the component, I am using a v-if to select between two different icons.

<v-icon name="play" class="controlIcons" v-if ="playControl" />
<v-icon name="pause" class="controlIcons" v-if ="!playControl" />
Run Code Online (Sandbox Code Playgroud)

Now, after some HTTP requests, I see the value of playControl change in Vue Devtools which confirms that the actual value is reflected in the component. However, I can't see the icons change due to the change in the value of playControl. Furthermore, everything works as expected if I refresh the screen.

My guess is that I'm messing up the reactivity somewhere. Thanks!

alx*_*lzt 7

您正在寻找https://vuejs.org/v2/guide/reactivity.html

for(var i = 0; i < getList.length; i++) {
    var newObject = Object.assign({}, getList[i]);
    this.$set(this.queuedItemList, i, newObject);
}
Run Code Online (Sandbox Code Playgroud)

另一种方法可能是更新父项列表 :key - 因此,重新渲染它 - 每次添加项目时:

</div :key="computedKey">    
    <Item v-for="(item, index) in queuedItemList" :key="index" :componentData="item" :index="index" @remove="removeItem"></Item>
</div>
Run Code Online (Sandbox Code Playgroud)

computed: {
    computedKey () {
        return this.queuedItemList.length
    }
}
Run Code Online (Sandbox Code Playgroud)