-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
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
Conversation
@@ -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) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
blank line here please :)
There was a problem hiding this comment.
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()
Since the
Vue.set
support to insert an item to array, theVue.delete
should also support to delete an item from array.Take the following code as an example:
The
Vue.delete(vm.lists, 1)
just delete the property1
from the array, but thevm.lists.length
is still be 2. Evaluatingobj.name
at the second time will caught a TypeError (undefined is not an object).