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】propsがArray・Objectの時default値にはファクトリー関数を使う

Posted at

エラー内容

propsのdefault値を空の配列にしたくて、こうすると、、、

props: {
    hoge: {
        type: Array,
        required: false, 
        default: [],
    }
}

こんなエラーが出てくる、、、

[Vue warn]: Invalid default value for prop "hoge": Props with type Object/Array must use a factory function to return the default value.

訳してみると、
「デフォルトにオブジェクトか配列を使うなら、ファクトリー関数を使ってね」
というもの。

解決方法

エラーに書いている通り、ファクトリー関数を使って、空の配列を生成するようにしてあげます。

props: {
  hoge: {
    type: Array,
    required: false, 
    default: () => []//空の配列を生成する関数を指定する
  }
}

以上です

参考

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?