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

180
Views
Cómo pasar accesorios de enfoque en componentes VueJS

Tengo una Input secundaria componente:

 <template> <div class="basic-input-outer" :style="styles"> <p class="paragraph-small">{{ title }}</p> <input ref="name" :type="type" class="basic-input"> </div> </template> <script> export default { name: "Input", props: ['type', 'styles', 'title', 'focus'], watch: { focus: function() { this.setFocus() } }, methods: { setFocus() { this.$refs.name.focus() } } } </script> <style lang="scss"> @import "../assets/css/components/Input"; </style>

Y tengo un componente principal donde trabajo con esta entrada:

 <div class="login-options"> <p class="choose" @click="chooseLogin('email')">With Email</p> <div class="vertical-line" /> <p class="choose" @click="chooseLogin('phone')">With Phone Number</p> </div> <div v-if="loginWithEmail"> <Input :focus="emailFocus" :title="'Email'" :type="'email'" /> </div> <div v-else> <Input :focus="phoneFocus" :title="'Phone number'" :type="'email'" /> </div> ... chooseLogin(option) { if (option === 'email') { this.loginWithEmail = true this.emailFocus = true } else { this.loginWithEmail = false this.phoneFocus = true } }

Entonces, el problema es que cuando activo la función, solo se enfoca una vez en un campo y luego se detiene. Quiero hacer que los accesorios de focus funcionen de esa manera, de modo que cuando se active, el campo se enfocará y funcionará no solo una vez, como en este caso.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Aquí está la solución. En realidad, fue porque v-if . v-if solo elimina los elementos de DOM, entonces, la mejor manera de resolver tales problemas es usar estilo condicional con display: none :

Input :

 <template> <div class="basic-input-outer" :style="styles"> <p class="paragraph-small">{{ title }}</p> <input ref="name" :type="type" class="basic-input"> </div> </template> <script> export default { name: "Input", props: ['type', 'styles', 'title', 'focus'], watch: { focus: function() { if (this.focus) this.$refs.name.focus() } } } </script> <style lang="scss"> @import "../assets/css/components/Input"; </style>

Y componente principal:

 <Input :style="[!loginWithEmail ? {'display': 'none'} : {'': ''}]" :focus="emailFocus" :title="'Email'" :type="'email'" /> <Input :style="[loginWithEmail ? {'display': 'none'} : {'': ''}]" :focus="phoneFocus" :title="'Phone number'" :type="'email'" /> ... chooseLogin(option) { if (option === 'email') { this.loginWithEmail = true this.emailFocus = true this.phoneFocus = false } else { this.loginWithEmail = false this.emailFocus = false this.phoneFocus = true } }
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!