日々の開発において、Nuxt 4 ロードマップと新アーキテクチャの利用頻度がますます高まっています。本記事ではその使い方、原理、最適化戦略を体系的に解説します。
クイックスタート
実際のプロジェクトでの使い方はより複雑になります:
javascript
import { ref, computed, watch, onMounted } from "vue";
export default {
setup() {
const count = ref(0);
const doubled = computed(() => count.value * 2);
watch(count, (newVal, oldVal) => {
console.log(`count: ${oldVal} -> ${newVal}`);
});
onMounted(() => {
console.log("コンポーネントがマウントされました");
});
return { count, doubled };
},
};
この方法により、コードのテスタビリティとスケーラビリティが向上します。
内部原理
以下は完全な例です:
javascript
import { reactive, toRefs, computed } from "vue";
function useCounter(initial = 0) {
const state = reactive({ count: initial, history: [initial] });
const doubled = computed(() => state.count * 2);
function increment() {
state.count++;
state.history.push(state.count);
}
return { ...toRefs(state), doubled, increment };
}
エッジケースの処理に注意してください。これは本番環境では非常に重要です。
実務での応用
重要なのはコアロジックを理解することです:
javascript
import { ref, computed, watch, onMounted } from "vue";
export default {
setup() {
const count = ref(0);
const doubled = computed(() => count.value * 2);
watch(count, (newVal, oldVal) => {
console.log(`count: ${oldVal} -> ${newVal}`);
});
onMounted(() => {
console.log("コンポーネントがマウントされました");
});
return { count, doubled };
},
};
パフォーマンス最適化は具体的なシナリオに合わせる必要があり、すべての状況で過度な最適化が必要なわけではありません。
パフォーマンス比較
以下の方法で改善できます:
javascript
import { reactive, toRefs, computed } from 'vue'
function useCounter(initial = 0) {
const state = reactive({ count: initial, history: [initial] })
const doubled = computed(() => state.count * 2)
function increment() {
state.count++
state.history.push(state.count)
}