-
-
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
Objects with an array as a prototype are not reactive #4049
Comments
I looked through the source code and I see now why this is happening. Basically, the code uses vue/src/core/observer/index.js Lines 44 to 52 in 341d0a0
|
Please read the doc. http://vuejs.org/api/#data
I just think |
@lgdexter |
@HerringtonDarkholme has a point. This isn't a purely plain object; it's using an array as a prototype. What a shame. Might be nice to add this in as a feature in the future. Let me know what you guys decide @phanan @yyx990803 |
The thing is |
I got a similar issue on another project and replaced |
I basically replaced the object with Would |
I'm working on adding an extension to a library of mine and I ran into this problem as well. My first guess was to also just override var $trigger = Rekord.Collection.prototype.trigger
Rekord.Collection.prototype.trigger = function (ev, args) {
const result = $trigger.apply(this, arguments)
const ob = this.__ob__
if (ob) {
switch (ev) {
case 'add':
ob.observeArray([args[0]])
break
case 'adds':
ob.observeArray(args[0])
break
case 'reset':
ob.observeArray(this.slice())
break
}
ob.dep.notify()
}
return result
} |
IMHO, the best two alternatives are:
|
There's also another gotcha - |
The objects you are creating are in fact array-like objects and not real arrays. Vue only supports native Arrays or arrays that are real, proper sub-class of Array (only available in ES2015+), which can pass the Also - custom array-like objects likely have far worse performance than real arrays, and I personally recommend against that pattern. |
Honestly don't know why Array subclass worked before. Refer to vuejs/vue#4049, vuejs/vue#6943, vuejs/vue#5893.
Honestly don't know why Array subclass worked before. Refer to vuejs/vue#4049, vuejs/vue#6943, vuejs/vue#5893.
Honestly don't know why Array subclass worked before. Refer to vuejs/vue#4049, vuejs/vue#6943, vuejs/vue#5893.
Vue.js version
2.0.3
Reproduction Link
https://jsfiddle.net/yMv7y/1860/
Steps to reproduce
What is Expected?
Both counters increment.
What is actually happening?
Very simply, Vue reactivity works for objects that are
[]
but not objects that have[]
as a prototype. This is happening in Vue 2.0.While this problem is easily circumvented by assigning a property in the object to be an array, it would be nice to be able to use objects that have an array as a prototype.
The text was updated successfully, but these errors were encountered: