LoginSignup
11
8

More than 3 years have passed since last update.

vueで「 Cannot read property 'hoge' of undefined」に悩まされた件。

Last updated at Posted at 2019-06-14

Vueで

 Cannot read property 'hoge' of undefined

というエラーに悩まされた。

配列の中にオブジェクトを格納しているdataで

data[index]["hoge"]

みたいな方法でアクセスしようとしているのに、なぜか上記のエラーが出る。

解決は、
data[index]がundefinedの場合、["hoge"]へはアクセスできないので上記エラーになっていた、というだけだった。

<span v-if="data[index]">{{data[index]["hoge"]}}</span>

とv-ifを挟んでやったら、無事エラーが回避された。

雑に作っちゃいけませんね。。

11
8
1

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
11
8