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

125
Views
Pasar accesorios que se usarán con estilo en el componente secundario - Vue

Tengo un botón que ejecuta la función startProcess que generará un número aleatorio. Este número aleatorio se pasará como accesorio a Child.vue que se usará con estilo. Busqué en algunas páginas "Cómo usar accesorios con estilo en vue". La solución fue usar computed , pero nada parece funcionar. Para una mejor comprensión, por favor revise el código.

PS Este es un código simplificado. template, script, style eliminados.

aplicación.vue

 <button @click="startProcess">Start</button> <Child v-if="toggleChild" :top="top" /> data() { return { toggleChild: false, top: 0 } }, methods: { startProcess() { this.toggleChild = !this.toggleChild; this.top = Math.random(); }; }

niño.vue

 <button @click="logTop">Log</button> props: { top: Number }, computed: { return { cssProps() { "--top": `${this.top}%`; }; }; }; .foo { top: var(--top); };
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

intente como el siguiente fragmento:

 Vue.component('Child', { template: ` <div class=""> <button @click="logTop" class="foo" :style="cssProps">Log</button> </div> `, props: { top: Number, col: String }, methods: { logTop() { console.log(this.top) } }, computed: { cssProps() { return { '--top': `${this.top}%`, '--col': this.col } } } }) new Vue({ el: '#demo', data() { return { toggleChild: false, top: 0, col: '' } }, methods: { startProcess() { this.toggleChild = !this.toggleChild; this.top = Math.random()*100; this.col = 'red' } } }) Vue.config.productionTip = false Vue.config.devtools = false
 .foo { position: absolute; top: var(--top); background-color: var(--col); };
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="demo"> <button @click="startProcess">Start</button> <Child v-if="toggleChild" :top="top" :col="col" /> </div>

over 4 years ago · Santiago Trujillo 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!