onMounted, onUnmounted など関数呼び出し型this がないので、this.count みたいな書き方はNGref, reactive, computed を使うsetup() の中、または <script setup> の中 でしかライフサイクルフックは使えないonMounted を setup の外で書いてしまう// ❌ エラーになる
onMounted(() => {
console.log('mounted!');
});
✅ 解決:必ず setup の中(または <script setup>)で書く!
<script setup lang="ts">
import { onMounted } from 'vue';
onMounted(() => {
console.log('mounted!');
});
</script>
this を使おうとしてしまう(クラス脳のクセ)// ❌ this.count は存在しない
onMounted(() => {
this.count++; // ❌ エラー
});
✅ ref を使う!
const count = ref(0);
onMounted(() => {
count.value++; // ✅
});
async なライフサイクルで Promise をちゃんと待たない