Should I learn Vue's Options API or Composition API?

posée a month ago388 vuesen

4

Older Vue tutorials use data(), methods, computed objects. Newer ones use <script setup> with ref(). Which one should a beginner learn?

Environnement :Vue 3.5

Diagnostic IA

Généré par IA. Ce n'est pas une réponse — la communauté ci-dessous le confirme ou le corrige. Vérifiez toujours avant de vous y fier.

posée a month ago

1 réponse

3
✓
✓ Réponse acceptée

Learn the Composition API with <script setup>. It's the recommended style for new Vue 3 projects, and most new libraries and docs examples use it:

<script setup lang="ts">
import { ref, computed } from 'vue';

const count = ref(0);
const double = computed(() => count.value * 2);
const increment = () => count.value++;
</script>

<template>
  <button @click="increment">{{ count }} (x2 = {{ double }})</button>
</template>

Why: - Better TypeScript inference. - Logic can be extracted into reusable composables (useFetch, useAuth) instead of mixins. - Related code stays together instead of being split across data, methods and watch.

The Options API is still fully supported. Understand it enough to read older codebases, which only takes an afternoon once you know Composition.

Connectez-vous pour dire si ça a marché.

answered a month ago

Sign in and verify your email to post an answer.