Bird
Raised Fist0
Vueframework~30 mins

Options API vs Composition API decision in Vue - Hands-On Comparison

Choose your learning style10 modes available
Options API vs Composition API decision
📖 Scenario: You are building a simple Vue 3 app to display a list of fruits and their colors. You want to practice two ways of writing Vue components: the Options API and the Composition API. This will help you understand how to organize your code and decide which style to use in your projects.
🎯 Goal: Create a Vue 3 component that shows a list of fruits with their colors. First, set up the data using the Options API. Then, add a configuration variable to filter fruits by color. Next, implement the filtering logic using the Composition API. Finally, complete the component template to display the filtered fruits.
📋 What You'll Learn
Use Vue 3 with the Options API for initial data setup
Add a filter color variable to select fruits by color
Use the Composition API to filter the fruits list based on the selected color
Complete the template to render the filtered fruits with their colors
Follow Vue 3 best practices with <script setup> and reactive variables
💡 Why This Matters
🌍 Real World
Vue developers often choose between Options API and Composition API to organize their code. Understanding both helps in maintaining and scaling projects.
💼 Career
Knowing how to write Vue components in both styles is valuable for frontend developer roles, especially when working on existing codebases or new projects.
Progress0 / 4 steps
1
DATA SETUP with Options API
Create a Vue component using the Options API. Define a data function that returns an object with a fruits array. The array should have these exact objects: { name: 'Apple', color: 'Red' }, { name: 'Banana', color: 'Yellow' }, and { name: 'Grape', color: 'Purple' }.
Vue
Hint

Remember, in the Options API, data is a function that returns an object with your reactive data.

2
CONFIGURATION: Add filterColor variable
Inside the same Vue component, add a new data property called filterColor and set it to the string 'Red'. This will be used to filter fruits by their color.
Vue
Hint

Add filterColor inside the object returned by the data function.

3
CORE LOGIC: Filter fruits with Composition API
Rewrite the component using the <script setup> block and the Composition API. Import ref and computed from 'vue'. Create a fruits ref with the same array of fruit objects. Create a filterColor ref set to 'Red'. Then create a filteredFruits computed property that returns fruits filtered by filterColor.value.
Vue
Hint

Use ref for reactive variables and computed for derived data in the Composition API.

4
COMPLETION: Display filtered fruits in template
In the template, use a <ul> element. Inside it, use v-for with fruit and index to loop over filteredFruits. For each fruit, display a <li> showing the fruit's name and color. Add a :key attribute with index for list rendering.
Vue
Hint

Use v-for to loop over filteredFruits and display each fruit's name and color inside a list item.

Practice

(1/5)
1. Which Vue API organizes component code by options like data, methods, and computed?
easy
A. Options API
B. Composition API
C. Both APIs organize code this way
D. Neither API uses options

Solution

  1. Step 1: Understand Options API structure

    The Options API groups code by options such as data, methods, and computed.
  2. Step 2: Compare with Composition API

    The Composition API organizes code inside the setup() function by logical features, not by options.
  3. Final Answer:

    Options API -> Option A
  4. Quick Check:

    Options API groups by options like data and methods [OK]
Hint: Options API groups by options, Composition API uses setup() [OK]
Common Mistakes:
  • Confusing Composition API with Options API structure
  • Thinking both APIs organize code the same way
  • Believing Options API uses setup() function
2. Which of the following is the correct way to declare a reactive state variable using the Composition API in Vue?
easy
A. const count = ref(0); inside setup()
B. data() { return { count: 0 } }
C. methods: { count() { return 0 } }
D. computed: { count: () => 0 }

Solution

  1. Step 1: Identify Composition API reactive declaration

    In Composition API, reactive state uses ref() inside setup(), e.g., const count = ref(0);.
  2. Step 2: Compare other options

    data() { return { count: 0 } } is Options API syntax, A and D are for methods and computed properties, not reactive refs.
  3. Final Answer:

    const count = ref(0); inside setup() -> Option A
  4. Quick Check:

    Use ref() in setup() for reactive state [OK]
Hint: Use ref() inside setup() for reactive state in Composition API [OK]
Common Mistakes:
  • Using data() syntax in Composition API
  • Confusing methods with reactive state
  • Trying to declare reactive state outside setup()
3. Given this Vue component using Composition API:
import { ref } from 'vue';
export default {
  setup() {
    const count = ref(0);
    function increment() {
      count.value++;
    }
    return { count, increment };
  }
}

What will be the value of count after calling increment() twice?
medium
A. 0
B. 1
C. 2
D. undefined

Solution

  1. Step 1: Understand ref and increment function

    count is a ref initialized to 0. Calling increment() increases count.value by 1 each time.
  2. Step 2: Calculate count after two increments

    Starting at 0, after two increments: 0 + 1 + 1 = 2.
  3. Final Answer:

    2 -> Option C
  4. Quick Check:

    Two increments add 2 to count.value [OK]
Hint: ref.value changes with increments; count.value++ twice = 2 [OK]
Common Mistakes:
  • Forgetting to use .value with ref
  • Assuming count is a plain number, not a ref
  • Thinking increment does not change count
4. Identify the error in this Vue component using Options API:
export default {
  data() {
    count: 0
  },
  methods: {
    increment() {
      this.count++;
    }
  }
}
medium
A. Incorrect method name increment
B. data() should be a property, not a function
C. Using this.count instead of count
D. Missing return statement in data()

Solution

  1. Step 1: Check data() function syntax

    In Options API, data() must return an object. Here, it lacks return keyword.
  2. Step 2: Verify other parts

    Method name and usage of this.count are correct. data() is correctly a function.
  3. Final Answer:

    Missing return statement in data() -> Option D
  4. Quick Check:

    data() must return an object [OK]
Hint: data() must return an object with return keyword [OK]
Common Mistakes:
  • Omitting return in data()
  • Confusing this.count with count
  • Thinking data() can be an object directly
5. You have a large Vue project with many components sharing logic like fetching data and handling loading states. Which API choice is best and why?
hard
A. Options API, because it separates code by options making it easier to read
B. Composition API, because it groups related logic together for better reuse
C. Options API, because it requires less code and is more modern
D. Composition API, because it avoids using setup() and is simpler

Solution

  1. Step 1: Consider project size and logic reuse

    Large projects with shared logic benefit from grouping related code together for reuse and clarity.
  2. Step 2: Compare API strengths

    Composition API allows grouping logic inside setup(), making reuse easier. Options API separates by options, which can scatter related logic.
  3. Final Answer:

    Composition API, because it groups related logic together for better reuse -> Option B
  4. Quick Check:

    Composition API groups logic for reuse in large projects [OK]
Hint: Use Composition API for reusable logic in big projects [OK]
Common Mistakes:
  • Choosing Options API for better reuse in large projects
  • Thinking Composition API avoids setup()
  • Believing Options API is more modern
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard