Skip to content
⚠️ This article was written in 2021. Some content may be outdated.

Vue 3 script-setup 糖衣構文

Vue 3 の script-setup 構文(シンタックスシュガー)は、フロントエンド開発での活用が広まっています。本記事では実際のプロジェクトをベースに、コア原理とベストプラクティスを掘り下げます。

基本的な使い方 ​

基本的な実装方法を見てみましょう:

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)
  }

  return { ...toRefs(state), doubled, increment }
}

境界条件の処理に注意してください。これは本番環境で非常に重要です。

まとめ ​

  • コードサンプルは参考用のみであり、ビジネスケースに応じて調整が必要です
  • Vue 3 の script-setup 構文は銀の弾丸ではなく、プロジェクトの規模や技術スタックに応じて選ぶ必要があります
  • 基礎的な原理を理解することは、APIを暗記することより重要です

MIT Licensed