-
-
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
Vue doesn't respect existing setters on Modelclasses #6845
Comments
Well, seems I've missed that bit of the documentation. Good point phanan. |
IMHO it's a bad idea to restrict data to plain objects. Working with components and props validation I'm using Modelclasses as type parameter like this:
This wouldn't work without typed models. |
Just to confirm: Does the addition of the |
Might be related to #5932. |
I prefer to revert this feature |
in加! in 原型判断与hasOwnProperty判断属性的区别是啥 |
class Model {
constructor() {
this.foo = '123'
this._bar = null
}
get bar() {
return this._bar;
}
set bar(newvalue) {
this._bar = newvalue;
}
}
new Vue({
data: function() {
return {
data: new Model()
}
}
}) 上面data字段初始化时是Model类的实例: {
foo: '123',
_bar: null,
__proto__: {
get bar: function() {
return this._bar
},
set bar: function(val) {
return this._bar = val
},
}
} 注意bar并不是实例的私有属性; 由于v-model使用的Vue.set方法; 会走到下边代码: defineReactive(ob.value, key, val)
ob.dep.notify() 这段代码相当于为实例重新定义了一个私有属性bar,这是不符合预期的,因为用户是把bar写在原型上了; 所以要把判断条件hasOwnProperty改为in判断: if (key in target && !(key in Object.prototype)) {
target[key] = val
return val
} 上边条件成立; |
Version
2.5.0
Reproduction link
https://codesandbox.io/s/vjojnn5w7l
Steps to reproduce
What is expected?
The existing setter "bar" should be used.
What is actually happening?
A new property "bar" is created.
The text was updated successfully, but these errors were encountered: