Vue.js 2:从数据对象中删除属性 [英] Vue.js 2: Delete property from data object

查看:32
本文介绍了Vue.js 2:从数据对象中删除属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从 Vue.js 数据对象(即关联数组)中删除属性/键,如下所示:

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; 没有更新 DOM
  • this.users.splice('foo', 1); 根本不起作用(可能只适用于数组,不适用于对象)
  • delete this.users.foo; is not updating the DOM
  • this.users.splice('foo', 1); is not working at all (probably only works on arrays, not on objects)

推荐答案

答案是:

Vue.delete(users, 'foo');

我花了一段时间才找到它,这就是我在这里发布它的原因;-)
https://github.com/vuejs/vue/issues/3368#issuecomment-236642919

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

这篇关于Vue.js 2:从数据对象中删除属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆