Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Be able to delete array item in Vue.delete #4747

Merged
merged 3 commits into from
Jan 19, 2017
Merged

Conversation

Hanks10100
Copy link
Contributor

Since the Vue.set support to insert an item to array, the Vue.delete should also support to delete an item from array.

Take the following code as an example:

const vm = new Vue({
  template: '<div><span v-for="obj in lists">{{obj.name}}</span></div>',
  data: {
    lists: [
      { name: 'A' },
      { name: 'B' }
    ]
  }
}).$mount()

Vue.delete(vm.lists, 1)
// vm.lists.length--

The Vue.delete(vm.lists, 1) just delete the property 1 from the array, but the vm.lists.length is still be 2. Evaluating obj.name at the second time will caught a TypeError (undefined is not an object).

@@ -218,7 +218,11 @@ export function set (obj: Array<any> | Object, key: any, val: any) {
/**
* Delete a property and trigger change if necessary.
*/
export function del (obj: Object, key: string) {
export function del (obj: Array<any> | Object, key: any) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

key is either a string or a number, isn't it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I just keep the type check be same with Vue.set

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, in that case, we should probably update both

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The conflict is the first argument of splice must be number, can't be string.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I must be missing something 😅 what is wrong with key: number | string

Copy link
Contributor Author

@Hanks10100 Hanks10100 Jan 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tried, if I set the key: number | string, the type check of obj.splice(key, 1) would be failed. Because the first argument of splice must be a number.

@@ -72,5 +72,29 @@ describe('Global API: set/delete', () => {
expect(vm.$el.innerHTML).toBe('')
}).then(done)
})
it('be able to delete an item in array', done => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blank line here please :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I will also remove the blank line after .$mount()

@yyx990803 yyx990803 merged commit 466e849 into vuejs:dev Jan 19, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants