简体   繁体   中英

@update:model-value isn't switching options in q-option-group (Vue3/Quasar)

I'm new in Quasar Framework and Vue.js . I wanted to use q-option-group for my simple application. However, model-value and @update:model-value didn't switch option from one to another. I know, I can use v-model but I can also use model-value instead of it by the quasar documentation.

I just wonder how it is used.

HTML

<div id="q-app" style="min-height: 100vh;">
  <div class="q-pa-lg">
    <q-option-group
      :model-value="selected_fruit"
      :options="fruits"
      color="dark"
      @update:model-value="value => { selected_fruit = value }"
    ></q-option-group>
  </div>
</div>

VUE

const { ref } = Vue

const app = Vue.createApp({
  setup () {
    return {
      selected_fruit: 'apple',
      fruits: [
        {
          label: 'Apple',
          value: 'apple'
        },
        {
          label: 'Banana',
          value: 'banana'
        },
        {
          label: 'Pear',
          value: 'pear'
        }
      ]
    }
  }
})

app.use(Quasar, { config: {} })
app.mount('#q-app')

Thank you for answers

You need to make it reactive with ref or reactive :

 const { ref } = Vue const app = Vue.createApp({ setup () { const fruits = ref([{label: 'Apple', value: 'apple'}, {label: 'Banana', value: 'banana'}, {label: 'Pear', value: 'pear'}]) const selected_fruit = ref('apple') return { selected_fruit, fruits} } }) app.use(Quasar, { config: {} }) app.mount('#q-app')
 <link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900|Material+Icons" rel="stylesheet" type="text/css"> <link href="https://cdn.jsdelivr.net/npm/quasar@2.5.5/dist/quasar.prod.css" rel="stylesheet" type="text/css"> <script src="https://cdn.jsdelivr.net/npm/vue@3/dist/vue.global.prod.js"></script> <script src="https://cdn.jsdelivr.net/npm/quasar@2.5.5/dist/quasar.umd.prod.js"></script> <div id="q-app" style="min-height: 100vh;"> <div class="q-pa-lg"> <q-option-group :model-value="selected_fruit" :options="fruits" color="dark" @update:model-value="value => { selected_fruit = value }" ></q-option-group> {{ selected_fruit }} </div> </div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM