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.

Nuxt.jsでdataプロパティの書き換えを変数で指定する

Posted at

やりたいこと

例えば下記のようなdataオブジェクトがあったとき

data: () => ({
    form: {
      name: '',
      email: '',
      birthday: ''
    },
  }),

nameを更新するときはこのように書くと思います。

this.form.name = 'xxxxx'

このnameの部分と変更する値を変数で渡してやりたいということがあり、フロント初心者の私はハマったので備忘録としてメモしておきます。

$setを使い、このように書ける。

this.$set(this.form, name, 'xxxxx')

これをmethodsで引数として受け取って処理しようとするとこんな感じで動いた。

updateItem (key, value) {
    this.$set(this.form, key, value)
},
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?