Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

182
Views
Lienzo responsivo vuejs

Estoy tratando de hacer que mi lienzo se ajuste a su contenedor, en un componente vue. Disparo resizeCanvas() en montado, pero la altura y el ancho del contenedor son 0. ¿Cómo puedo tener un lienzo que se ajuste a su contenedor, de modo que pueda cambiar el tamaño del contenedor usando css y actualizará el lienzo? (Perdón por mi inglés, no hablo con fluidez)

 <template lang="html"> <div class="container" ref="container"> <canvas ref="canvas" :width="width" :height="height"></canvas> </div> </template> <script> export default { name: "monster", data () { return { width: 512, height: 512 } } methods: { resizeCanvas(){ console.log(this.$refs.container.offsetWidth); // logs 0 this.width = this.$refs.container.offsetWidth this.height = this.$refs.container.offsetWidth } }, mounted () { console.log(this.$refs.container.offsetWidth) // logs 0 window.addEventListener("resize", this.resizeCanvas); this.resizeCanvas() }, unmounted() { window.removeEventListener("resize", this.resizeCanvas); }, } </script> <style lang="css" scoped> .container { width: 100%; height: 100% } </style>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

El enlace del ciclo de vida montado en VueJS no garantiza que el DOM esté listo: deberá esperar this.$nextTick() y luego verificar las dimensiones de su elemento contenedor.

Puede hacerlo moviendo la lógica a la devolución de llamada de nextTick, es decir:

 mounted () { this.$nextTick(() => { console.log(this.$refs.container.offsetWidth); window.addEventListener("resize", this.resizeCanvas); this.resizeCanvas(); }); },

Si está familiarizado con la forma async/await de hacer las cosas, también puede hacer esto:

 async mounted () { await this.$nextTick(); console.log(this.$refs.container.offsetWidth); window.addEventListener("resize", this.resizeCanvas); this.resizeCanvas(); },

Otra posibilidad es delegar esta responsabilidad de "esperar a que DOM esté listo" a la función resizeCanvas , para que tenga una abstracción completa y una separación de preocupaciones:

 methods: { async resizeCanvas(){ await this.$nextTick(); this.width = this.$refs.container.offsetWidth this.height = this.$refs.container.offsetWidth } }, mounted () { window.addEventListener("resize", this.resizeCanvas); this.resizeCanvas(); },
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!