question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

vue do not update correctly when use Array.prototype.splice.call to modify data

See original GitHub issue

Vue.js version

1.0.21

Reproduction Link

Steps to reproduce

just click the apple or orange

in the first example without using call(this.items.splice), you can see that the clicked item is deleted from page. In the second example with using call(Array.prototype.splice.call), the clicked item is still here, but you can see it deleted from items array in the console log.

What is Expected?

I want to know why using Array.prototype.splice.call make vue not work. i search the call function on MDN.

** the description of it **

the call() method calls a function with a given this value and arguments provided individually.

so, i can’t figure out why, any one could explain it, thanks~

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
akaibukaicommented, Jul 13, 2017

Hello @Toilal, I just hit the same problem with lodash _.remove() function using VueJS… Another workaround could be to use the replacing an array method with the lodash _.reject() function which does not mutate the array.

4reactions
prog-rajkamalcommented, Aug 12, 2016

The reason is that Array.prototype.splice and this.items.splice are different functions.

When Vue makes an array reactive, it modifies the splice function, so that Vue is notified when array is changed.

So when you use Array.prototype.splice.call, you modify the array, but Vue is not notiifed. but when you use this.items.splice, Vue is notified.

Source : Vuejs Guide

Read more comments on GitHub >

github_iconTop Results From Across the Web

VueJs - DOM not updating on array mutation - Stack Overflow
selectableItems renders correctly initially, but when I mutate the array, the dom isn't updating. Although the actual array is being modified ...
Read more >
Array.prototype.splice() - JavaScript - MDN Web Docs
The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.
Read more >
Update Array and Object in Vuejs. Array Change Detection
Array Change Detection. When you modify an Array by directly setting an index (e.g. arr[0] = val ) or modifying its length property....
Read more >
Understanding the New Reactivity System in Vue 3 - SitePoint
Learn why Vue 3's new, feature-rich reactivity API is far more flexible and ... we just use the JavaScript built-in splice() array method....
Read more >
Migration from Vue 1.x
Rest assured, this is not something you have to read from top to bottom! ... Instead, you should use an array of objects...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found