gpt4 book ai didi

javascript - Vue.js 正确重新渲染数组项

转载 作者:搜寻专家 更新时间:2023-10-30 22:54:56 24 4
gpt4 key购买 nike

我在 vue.js 2 中有一个结构如下的数组:

data() {
return {
ports: [
{ id: 1, name: "example", "age": 10, scores: [ {index: 1, value: 100}, {index: 2, value: 200} ]},
{ id: 2, name: "example", "age": 10, scores: [ {index: 1, value: 100}, {index: 2, value: 200} ]}
{ id: 3, name: "example", "age": 10, scores: [ {index: 1, value: 100}, {index: 2, value: 200} ]}
{ id: 4, name: "example", "age": 10, scores: [ {index: 1, value: 100}, {index: 2, value: 200} ]}
]
}
}

我想用 id 1 替换 portscores。我知道我可以像这样替换整个端口:

Vue.set(this.ports, 0, newPort);

但是 port 对象上的 scores 不再是 reactive 并且它不会在我的子组件中 rerender !

我怎样才能做到这一点?

最佳答案

But then scores on the port object is not reactive anymore and it does not rerender in my subcomponents!

scores 属性将是响应式的,前提是它在 Vue.set 调用时存在于 newPort 对象上。

I want to replace the scores of port with id 1.

为什么要替换整个端口对象而不是仅仅替换分数?这将起作用:

this.ports.find(port => port.id === 1).scores = newScores

关于javascript - Vue.js 正确重新渲染数组项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52234449/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com