vue do not update correctly when use Array.prototype.splice.call to modify data
See original GitHub issueVue.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:
- Created 7 years ago
- Comments:8 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.The reason is that
Array.prototype.splice
andthis.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 usethis.items.splice
, Vue is notified.Source : Vuejs Guide