Reputation: 29
I have the following code within my component:
computed: {
gridWidth() { return (this.$el && this.$el.offsetWidth) },
gridHeight() { return (this.$el && this.$el.offsetHeight) },
},
mounted(){
console.log(this.$el && this.$el.offsetWidth, this.gridWidth, this.$el && this.$el.offsetHeight, this.gridHeight)
},
The console output is:
1766 undefined 931 undefined
Why are the computed properties undefined?
Would be a watcher the right way? Why and how to realize?
Upvotes: 0
Views: 882
Reputation: 1003
This is basically related to how reactivity in Vue works. By default your DOM is not continually providing updated values.
You can either play around with vm.$nextTick and vm.$watch to basically attach a watcher after the rendering has been finished, or you can use a small library which does exactly what you are looking for:
Upvotes: 1