[Vue warn]: Property or method is not defined on the instance
Vueでテンプレート内の未定義プロパティを参照した際の警告
概要
Vue.jsでテンプレート内で参照しているプロパティやメソッドが、コンポーネントのdata、computed、methods等に定義されていない場合に発生する警告です。
エラーメッセージ
[Vue warn]: Property or method "x" is not defined on the instance but referenced during render.
Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property.
原因
- プロパティの定義忘れ: dataやcomputedに定義していない
- タイプミス: プロパティ名のスペルミス
- スコープの問題: v-for内の変数を外で参照
- Composition APIの返却忘れ: setup()でreturnしていない
- Vueバージョンの違い: Vue 2とVue 3の構文の違い
解決策
1. dataに定義(Options API)
| |
2. setup()でreturn(Composition API)
| |
| |
3. propsの定義
| |
4. computedプロパティ
| |
5. メソッドの定義
| |
6. v-forのスコープ
| |
7. 条件付きレンダリング
| |
よくある間違い
- Vue 2の
this.xxxをVue 3のscript setupで使おうとする - computedをmethodsに書く(またはその逆)
- propsを
definePropsなしで使用する - インポートしたコンポーネントを登録し忘れる
関連エラー
参考リンク
Vue.js の他のエラー
この記事は役に立ちましたか?