1
1

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 1 year has passed since last update.

【vue/Nuxt】オブジェクトの追加

Last updated at Posted at 2022-10-26

オブジェクトの追加には注意が必要

APIで取得してきたオブジェクトをdataで宣言済みのオブジェクトに追加した際に、Cannot read properties of undefinedエラーが出たり、リアクティブに操作できなくてはまったので備忘録。

結論、オブジェクトの追加は$setなどを使う

data() {
      return {
        test: {
          default: {},
        },
      };
    },
    mounted() {
      this.$set(this.test, "add", {}); // -> リアクティブなadd{}が追加される
    },

単純に=を使ってオブジェクトの追加をしていたのが原因だった。dataの中で宣言しておくか、$setを使って、get/setを持つリアクティブデータとして追加して解決。

解説

vueインスタンスのdataの中に宣言されたものはget/setを持ち、何かしら変更があったら感知ができるリアクティブなデータになる。

image.png

dataの中で宣言したものはリアクティブなものだが、=などで後から追加されたオブジェクトはget/setを持たない、非リアクティブなデータ。
下記サンプルコードで確認してみる。

data() {
      return {
        test: {
          default: {},
        },
      };
    },
    mounted() {
      this.test.add = {};
      this.$set(this.test, "add2", {});
      console.log(this.test);
    },

出力結果
スクリーンショット 2022-10-26 11.13.04.png

dataの中で宣言していた、default: {}はget/setを持つリアクティブデータ。
=で単純に追加したadd: {}はget/setを持たない非リアクティブデータ。
$setを使って追加したadd2: {}はget/setを持つリアクティブデータであることがわかる。

まとめ

リアクティブなデータを追加していかないと、予期せぬエラーになる。
なのでデータを追加する際には$setを使って、get/setを持つリアクティブデータで追加する。
公式を見るとObject.assign()_.extend()でも追加できるよう。

公式:リアクティブの探求

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?