vue.js - 从 vuex 存储中排序数组(无限渲染警告)
问题描述
我正在从 vuex 存储中检索一组对象,并尝试在我的一个组件中对该数组进行排序(我不想在存储中对数组进行排序)。但是我的浏览器出现错误infinite update loop in a component render function
。这里发生了什么,我该如何解决这个问题?
<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 []
}
},
}
)
解决方案
随着this.currentItemList.sort
您在计算属性中改变反应性数据 - 这将触发组件始终重新渲染......不要改变计算属性中的数据。相反,请确保对数组的副本进行排序:this.currentItemList.slice().sort(...)
推荐阅读
- google-apps-script - 如何将每个值添加到firebase中的数据对象?
- ansible - 将交互式值/密码传递给 Ansible
- batch-file - 递增编号最高的子键
- python - 我在使用 Tensorflow object_detection 的 python 项目中遇到此错误
- blockchain - 如何验证部署到 EtherScan 以更新图标的克隆的最小代理合同 (EIP1167)?
- ios - Swift 包管理器 - 在测试目标中使用与主目标相同的故事板
- javascript - 如何根据屏幕宽度和点击更改幻灯片的 img src
- neo4j - neo4j-admin 加载时无法找到 Java 运行时
- qt - 如何禁用虚拟键盘?
- flutter - Flutter Web:从 PC 加载 Web 寻呼机时没有“Access-Control-Allow-Origin”问题