LoginSignup
0
0

More than 3 years have passed since last update.

vueで動的に生成したフォームのバインディング

Posted at

構成

  • vue2.61
  • vuetify2.26

実現したこと

v-forを使って動的に生成したそれぞれのインプット項目をリアクティブに扱う

無駄にはまったところ

余計なv-modelをつけるとインプットイベントでv-forで展開したインプット項目が初期化されてしまう。

実装したもの

sample.vue
<template>
  <v-container v-for="item in list" :key="item.id"><!-- リアクティブだからといってここに v-model="list"としてはいけない-->
      <v-row>
          <v-col>
              <v-textfield
                  label="ユーザーアドレス"
                  v-model="item.email"
              ></v-textfield>
          </v-col>
          <v-col>
              <v-textfield
                  label="ユーザー名"
                  v-model="item.name"
              ></v-textfield>
          </v-col>
      <v-row>
  </v-container>
</template>

<script>
  export default {
    name: 'sample',
    data: () => ({
      list: [
        {id: 1, email: 'sample1@eamil.com', name: 'sample1'},
        {id: 2, email: 'sample2@eamil.com', name: 'sample2'}
      ],
    }),
  }
</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