Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vue.js 从vuex存储区排序数组(无限渲染警告)_Vue.js - Fatal编程技术网

Vue.js 从vuex存储区排序数组(无限渲染警告)

Vue.js 从vuex存储区排序数组(无限渲染警告),vue.js,Vue.js,我正在从vuex存储中检索一个对象数组,并尝试在我的一个组件中对该数组进行排序(我不想在存储中对数组进行排序)。但我在浏览器中的组件渲染函数中得到一个错误无限更新循环。这里发生了什么,我如何解决 <template> <div v-for="(item, i) in itemList"> {{item.description}} </div> </template> computed: Object.assign({},

我正在从vuex存储中检索一个对象数组,并尝试在我的一个组件中对该数组进行排序(我不想在存储中对数组进行排序)。但我在浏览器中的组件渲染函数中得到一个错误
无限更新循环。这里发生了什么,我如何解决

<template>
  <div
    v-for="(item, i) in itemList">
    {{item.description}}
  </div>
</template>


computed: Object.assign({},
  mapGetters({
    currentItemList: "item/current",
  }),
  {
   itemList() {
     if(this.currentItemList != undefined) {
       return this.currentItemList.sort(function(a, b) {
           var textA = a.description.toUpperCase()
           var textB = b.description.toUpperCase()
           return (textA < textB) ? -1 : (textA > textB) ? 1 : 0
         })
      }
      else {
        return []
      }
    },
  }
)

{{item.description}
computed:Object.assign({},
制图员({
currentItemList:“项目/当前”,
}),
{
项目列表(){
if(this.currentItemList!=未定义){
返回此.currentItemList.sort(函数(a,b){
var textA=a.description.toUpperCase()
var textB=b.description.toUpperCase()
返回(textAtextB)?1:0
})
}
否则{
返回[]
}
},
}
)

排序
将在适当位置对数组进行变异。您可以在组件中创建项目列表的本地副本,然后对副本进行排序以避免副作用:

itemList(){
if(this.currentItemList!=未定义){
var localItemList=JSON.parse(JSON.stringify(this.currentItemList))
返回localItemList.sort(…)

使用
this.currentItemList.sort
您正在变异计算属性中的反应数据-这将触发组件始终重新排序…不要变异计算属性中的数据。请确保在数组副本上排序:
this.currentItemList.slice().sort(…)

Jns的解决方案使用
Array.prototype.slice()
(请参阅)不仅比在JSON之间编码和解析对象快得多,而且还得到了更广泛的支持。