MeWrite Docs

[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.

原因

  1. プロパティの定義忘れ: dataやcomputedに定義していない
  2. タイプミス: プロパティ名のスペルミス
  3. スコープの問題: v-for内の変数を外で参照
  4. Composition APIの返却忘れ: setup()でreturnしていない
  5. Vueバージョンの違い: Vue 2とVue 3の構文の違い

解決策

1. dataに定義(Options API)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<template>
  <p>{{ message }}</p>
</template>

<script>
export default {
  // Bad: dataに定義していない
  // Good
  data() {
    return {
      message: 'Hello'
    }
  }
}
</script>

2. setup()でreturn(Composition API)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<template>
  <p>{{ count }}</p>
  <button @click="increment">+</button>
</template>

<script setup>
import { ref } from 'vue'

// script setupでは自動的にexpose
const count = ref(0)
const increment = () => count.value++
</script>
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
<!-- setup()関数を使う場合 -->
<script>
import { ref } from 'vue'

export default {
  setup() {
    const count = ref(0)

    // Bad: returnしていない
    // Good: 必ずreturn
    return {
      count
    }
  }
}
</script>

3. propsの定義

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<template>
  <p>{{ title }}</p>
</template>

<script setup>
// propsを定義
defineProps({
  title: String
})
</script>

4. computedプロパティ

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<template>
  <p>{{ fullName }}</p>
</template>

<script setup>
import { ref, computed } from 'vue'

const firstName = ref('John')
const lastName = ref('Doe')

// computedで定義
const fullName = computed(() => `${firstName.value} ${lastName.value}`)
</script>

5. メソッドの定義

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<template>
  <button @click="handleClick">Click</button>
</template>

<script>
export default {
  methods: {
    handleClick() {
      console.log('clicked')
    }
  }
}
</script>

6. v-forのスコープ

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<template>
  <!-- Bad: itemはv-forのスコープ外 -->
  <p>{{ item }}</p>

  <!-- Good: v-for内で使用 -->
  <ul>
    <li v-for="item in items" :key="item.id">
      {{ item.name }}
    </li>
  </ul>
</template>

7. 条件付きレンダリング

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<template>
  <!-- Bad: userが未定義の可能性 -->
  <p>{{ user.name }}</p>

  <!-- Good: 存在確認 -->
  <p v-if="user">{{ user.name }}</p>

  <!-- Good: Optional chaining -->
  <p>{{ user?.name }}</p>
</template>

よくある間違い

  • Vue 2のthis.xxxをVue 3のscript setupで使おうとする
  • computedをmethodsに書く(またはその逆)
  • propsをdefinePropsなしで使用する
  • インポートしたコンポーネントを登録し忘れる

関連エラー

参考リンク

Vue.js の他のエラー

最終更新: 2025-12-13