How can you delete a property/key from a Vue.js data object (i.e. associative array) like this:
var vm = new Vue({
data: {
users: {
foo : { firstName: ..., lastName: ... },
bar : { firstName: ..., lastName: ... }
}
},
methods: {
someFunction : function ()
{
// how to remove `users.foo`?
}
}
});
Googling around, I found these two ways, but both don't work:
delete this.users.foo;
is not updating the DOMthis.users.splice('foo', 1);
is not working at all (probably only works on arrays, not on objects)
The answer is:
It took me a while to find it, that's why I'm posting it here ;-)
https://github.com/vuejs/vue/issues/3368#issuecomment-236642919
It's important to know that
vm.$delete
is a alias forVue.delete
and if you try something likethis.delete()
you will get a error. So in your example the answer would be:or
https://vuejs.org/v2/guide/migration.html#vm-delete-changed
You have to create a new copy of the object so Vue be able to know that there are changes:
ES6
or without spread operator it would be