0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

VuetifyのClearableはtextfieldとselectで戻り値が違う

Posted at

TL;TR

textfieldの場合にはnull
selectの場合にはundefined

何が問題なの?

人によっては問題ではないのかもしれませんが、axiosでポストする際に、オブジェクトのバリューがundefinedだとサーバーサイドにそもそも送信されません。

axios.post('http://localhost:3000/api', {sample: 'a', test: undefined})

//Request Body {sample: 'a'}

そのため単純にv-selectにてクリアブルを使用していると、そのプロパティに関しては、まず存在チェックをかける必要があり、その上で値の確認をする必要があります。
テキストフィールドの場合と挙動が違うのとあらかじめ設定しておいたプロパティに存在チェックをかけなければいけないのも面倒だったので、selectに関してもクリアしたときにnullを返すように修正してみました。

<template>
  <div>
    <v-text-field
      label="sample"
      v-model="form.sampleText"
     ></v-text-field>
     <v-select
       label="select"
       v-model="form.sampleSelect"
       :items="selection"
       append-icon="close"
       @click:append="form.sampleSelect = null"
      ></v-select>
  </div>
</template>
<script>
export default {
  data: () => ({
    form: {
      sampleText: '',
      sampleSelect: null,
    }    
  }) 
}
</script>
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?