0

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?

1
  • Did you try this in your mounted.......this.$nextTick(() => { console.log(this.$el && this.$el.offsetWidth, this.$el && this.$el.offsetHeight, this.gridHeight)}) Commented Jan 14, 2021 at 11:32

1 Answer 1

1

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:

vue-resize-directive

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Now, I realized it with the resize event.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.