LoginSignup
6
0

More than 3 years have passed since last update.

【Vuetify】v-comboboxをアクティブ状態で送信ボタン押すと以前の値が取得される問題

Last updated at Posted at 2020-08-01

再現手順
①v-comboboxで手入力する
②v-comboboxをアクティブ状態のまま送信ボタン押下
→以前の入力データが取得されてしまう

問題のコード



上記でコンボボックスに何か入力(項目選択ではない)してアクティブ状態のまま送信ボタンを押してみてください
アラートに何も表示されませんが、2回目送信ボタンを押すと表示されます。

.vue
<div id="app">
  <v-app>
   <v-combobox
               v-model="userValue"
               :items="userItems"
               ref="userComboBox"
               >
     </v-combobox>
    <v-btn color="primary" dark large @click="submit()">送信</v-btn>
  </v-app>
</div>
.js
new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  data () {
    return {
      userValue: "",
      userItems: [
        "A","B","C"
      ],
    }
  },
  methods:{
    submit(){
      alert(this.userValue);
    }
  },
})

submit関数内のconsole.logで出力されるデータは現在入力されているデータではなく、
以前のデータが取得されてしまう。
v-comboboxは非アクティブにならないと入力情報がv-modelに伝わらない。
v-text-fieldではこの現象はもちろん発生しない。v-modelへの反映は入力されたら即時行われる為。

解決法はコンボボックスを非アクティブ(blur)してnextTickを使用する

.js
 submit(){
      this.$refs.userComboBox.blur();
       this.$nextTick(() => {
         console.log(this.userValue);
       })
    }
6
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
0