Can someone help me with an issue I'm having where a template element is not reacting to an added property of an array element? Here's the example:
<div v-for="user in users"
v-bind:key="user.id"
v-bind:name="user.name">
{{user.name}}
<div class="warn" v-if="user.hasWarning">Warning!</div>
</div>
I want to display a warning whenever hasWarning: true
is added to user
data:() {
users: [
{id: 1, name: "Foo"}
]
},
methods:{
showWarning: function(id) {
users.forEach(user => {
if (user.id == id) {
user.hasWarning = true;
}
});
}
}
Even though I can see hasWarning
being added to the user from both the console and Vue developer tools after calling showWarning(id)
, it doesn't render
<div class class="warn" v-if="user.hasWarning"..
. It works fine if my user is set up as {id: 1, name: "foo", hasWarning: false}
, so why isn't it working for the added property? Should I avoid this approach altogether?