-
Notifications
You must be signed in to change notification settings - Fork 432
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
Mutations of data in arrow functions declared as instance variables are ignored #67
Comments
Thank you for reporting this issue. I'm not sure if we can solve this problem. But we can work around this problem by just using methods instead of arrow function. Note that Vue binds vm instance as import Vue from 'vue'
import Component from 'vue-class-component'
@Component
export default class FollowWindowSize extends Vue {
width = window.innerWidth
height = window.innerHeight
onResize() {
this.width = window.innerWidth
this.height = window.innerHeight
}
mounted() {
window.addEventListener('resize', this.onResize)
}
destroyed() {
window.removeEventListener('resize', this.onResize)
}
} |
I don't think this is fixable. Recommended solution is just declaring onResize as method. Vue will automatically bind The reason we cannot support this is below: Class syntax does not allow The |
As @HerringtonDarkholme explained, this is unfixable. I think we should mention about this behavior in docs. |
Isn't it possible to change this behavior? I think there are a few problems resulting from this behavior.
With the following modification of vue-class-component, https://github.com/kimamula/vue-class-component/pull/1/files
@Component({ data: () => ({
width: window.innerWidth,
height: window.innerHeight
}) })
export default class FollowWindowSize extends Vue {
onResize = () => {
this.width = window.innerWidth
this.height = window.innerHeight
}
mounted() {
window.addEventListener('resize', this.onResize)
}
destroyed() {
window.removeEventListener('resize', this.onResize)
}
} This implementation resolves the problems listed above. It is possible to change the behavior depending on whether |
I also want to note that #68 is also resolved in the above implementation. |
Thank you for your suggestion but it is breaking change, unfortunately. I'm afraid I cannot find valid reason to support the syntax since we can just define it as methods in that case. I understand your concern for constructor instantiation, however there is also a motivation that would like to define vm data as class properties. I think the current implementation is balanced - predictable and convenient. All properties will be processed as vm FYI: We cannot inject any value as |
Docs is updated. |
All right, I understand the philosophy of vue-class-component. |
@ktsn : Following up on this. |
In the following example, mutations of
this.width
andthis.height
inonResize
do not actually change the size of the div element.This is because, after the babel transpilation,
onResize
is declared inside constructor, which is executed before the wrapping of the data with getters and setters by Vue.js take place, and therefore mutations of data inonResize
are not handled by these setters.I confirmed that this is also the case when TypeScript is used.
The text was updated successfully, but these errors were encountered: