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

342
Views
Vue 3 Select-Option, haga que la primera opción sea la predeterminada

Estoy tratando de hacer que la primera opción de selección sea predeterminada, por lo tanto, se muestra de inmediato cuando se carga la página. Pensé que, dado que v-bind:selected es un atributo booleano, podría usar algo simple como index === 0 para seleccionar el primero de forma predeterminada, pero esto no parece funcionar y no hay ninguna opción seleccionada en la carga de la página. He depurado y los índices se incrementan normalmente, no pasa nada extraño allí. ¿Hay algo tonto que me estoy perdiendo tal vez? selectedThingId es una referencia con un valor predeterminado de cero. ¡También busqué en el html cambiando este número y nunca se seleccionó nada!

Aquí está el código de la plantilla:

 <select name="select" id="select" v-model="selectedThingId" @change="onChangeFunction"> <option v-for="(thing, index) in things" :value="thing.id" :key="thing.id" :selected="index === 0" > {{ thing }} </option> </select>
about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

Intente configurar el identificador de cosa selectedThingId en el gancho onMounted :

 const { ref, reactive, onMounted } = Vue const app = Vue.createApp({ setup() { const selectedThingId = ref(null) const things = reactive([{id: 3, name: 'aaa'}, {id: 5, name: 'bbb'}, {id: 9, name: 'ccc'}]) onMounted(() => selectedThingId.value = things[0].id) const onChangeFunction =() => {} return { things, selectedThingId, onChangeFunction } } }) app.mount('#demo')
 <script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script> <div id="demo"> <select name="select" id="select" v-model="selectedThingId" @change="onChangeFunction"> <option v-for="(thing, index) in things" :key="thing.id" :value="thing.id" > {{ thing.name }} </option> </select> {{ selectedThingId }} </div>

about 4 years ago · Santiago Gelvez 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!