Vue: Deleting object key with this.$delete doesn't work properly

Created on 25 Jul 2016  路  2Comments  路  Source: vuejs/vue

Vue.js version

1.0.26

Reproduction Link

https://jsfiddle.net/xL4ak6en/2/

Steps to reproduce

new Vue({
  el: '#app',
  data () {
    return {test: {firstKey: 1}}
  },
  template: `<div><button @click="testSet">Set</button><button @click="testDelete">Delete</button><span>{{ test | json }}</span></div>`,
  methods: {
    testSet: function () {
      this.$set('test.secondKey', 2)
    },
    testDelete: function () {
      this.$delete('test', 'secondKey')
      //this.$delete('test.secondKey')
      //Vue.delete('test', 'secondKey')
    }
  }

})

What is Expected?

When you click the "Set" button it creates test.secondKey: 2. Then when you click "Delete" button it should delete test.secondKey

What is actually happening?

It deletes the whole test object. The other 2 commented variants do nothing.

Most helpful comment

vm.$delete only supports deleting root level keys. You should use Vue.delete(obj, key) instead.

http://vuejs.org/api/#vm-delete

All 2 comments

vm.$delete only supports deleting root level keys. You should use Vue.delete(obj, key) instead.

http://vuejs.org/api/#vm-delete

Hey @yyx990803
Thanks just realized that i was doing Vue.delete('test', 'secondKey') instead of Vue.delete(this.test, 'secondKey')
Thanks!
you can close this one.. Hope it's helpful for others.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

aviggngyv picture aviggngyv  路  3Comments

robertleeplummerjr picture robertleeplummerjr  路  3Comments

loki0609 picture loki0609  路  3Comments

hiendv picture hiendv  路  3Comments

bdedardel picture bdedardel  路  3Comments