I want to know why I changed the specific item of an array and the page doesn't update. I know doc of vue.js points out that:
Due to limitations in JavaScript, Vue cannot detect the following changes to an array:
When you directly set an item with the index, e.g.
vm.items[indexOfItem] = newValue.
It says we should do that, but I don't know why. And I've found a similar question(Vue computed issue - when will it compute again) about that.
Here is some code from the question above:
// works well
data() {
return {
cart: {
item: {
nums: 10,
price: 10,
},
},
}
},
computed: {
total() {
return this.cart.item.nums * this.cart.item.price
},
},
methods: {
set() {
//why it worked.
this.cart.item = {
nums: 5,
price: 5,
}
},
},
// oops! not working!
data() {
return {
cart: [
{
nums: 10,
price: 10,
},
],
}
},
computed: {
total() {
return this.cart[0].nums * this.cart[0].price
},
},
methods: {
set() {
this.cart[0] = {
nums: 5,
price: 5,
}
},
},
I'm confused about the answer from the question:
total will be recalculated if
this.cartis marked as changed,this.cart[0]is marked as changed or ifthis.cart[0].numsorthis.cart[0].priceis changed. The problem is that you are replacing the object inthis.cart[0]. This means thatthis.cart[0].priceand nums do not change, because those still point to the old object.
If I have replaced the object in this.cart[0], why this.cart[0] isn't marked as changed? why this.cart[0].price and nums still point to old object? I have changed the this.cart[0]! right?
And why in the first situation it works well? also replace the object . What's the difference between the two scenarios?