0
0
Vueframework~20 mins

Why API integration matters in Vue - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
API Integration Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why do we use APIs in Vue applications?

Imagine you want your Vue app to show weather data from an online service. Why is using an API important here?

AAPIs are only used to style the app with colors and fonts.
BAPIs let your app get fresh data from other services without storing it all inside the app.
CAPIs make your app run faster by skipping all network requests.
DAPIs replace Vue components with pre-built templates.
Attempts:
2 left
💡 Hint

Think about how your app can show new information without updating the app itself.

component_behavior
intermediate
2:00remaining
What happens when a Vue component fetches API data?

Look at this Vue component snippet that fetches user info from an API. What will the component show after the data loads?

Vue
<script setup>
import { ref, onMounted } from 'vue'
const user = ref(null)
onMounted(async () => {
  const res = await fetch('https://api.example.com/user/1')
  user.value = await res.json()
})
</script>
<template>
  <div>
    <p v-if="user">Name: {{ user.name }}</p>
    <p v-else>Loading...</p>
  </div>
</template>
AIt shows nothing because user is null and no fallback is provided.
BIt shows 'Name: null' immediately and never changes.
CIt shows an error because fetch is not allowed in Vue components.
DIt first shows 'Loading...' then updates to 'Name: [user's name]' after data loads.
Attempts:
2 left
💡 Hint

Think about what happens before and after the API call finishes.

📝 Syntax
advanced
2:00remaining
Which option correctly handles API errors in Vue?

Choose the code snippet that properly catches errors when fetching API data inside a Vue component.

Vue
<script setup>
import { ref, onMounted } from 'vue'
const data = ref(null)
const error = ref(null)
onMounted(async () => {
  try {
    const res = await fetch('https://api.example.com/data')
    if (!res.ok) throw new Error('Network error')
    data.value = await res.json()
  } catch (e) {
    error.value = e.message
  }
})
</script>
<template>
  <div>
    <p v-if="error">Error: {{ error }}</p>
    <p v-else-if="data">Data loaded</p>
    <p v-else>Loading...</p>
  </div>
</template>
A
&lt;script setup&gt;
import { ref, onMounted } from 'vue'
const data = ref(null)
const error = ref(null)
onMounted(() =&gt; {
  fetch('https://api.example.com/data')
    .then(res =&gt; res.json())
    .then(json =&gt; data.value = json)
    .catch(e =&gt; error.value = e.message)
})
&lt;/script&gt;
B
&lt;script setup&gt;
import { ref, onMounted } from 'vue'
const data = ref(null)
const error = ref(null)
onMounted(async () =&gt; {
  const res = await fetch('https://api.example.com/data')
  data.value = await res.json()
})
&lt;/script&gt;
C
&lt;script setup&gt;
import { ref, onMounted } from 'vue'
const data = ref(null)
const error = ref(null)
onMounted(async () =&gt; {
  try {
    const res = await fetch('https://api.example.com/data')
    if (!res.ok) throw new Error('Network error')
    data.value = await res.json()
  } catch (e) {
    error.value = e.message
  }
})
&lt;/script&gt;
D
&lt;script setup&gt;
import { ref, onMounted } from 'vue'
const data = ref(null)
const error = ref(null)
onMounted(async () =&gt; {
  try {
    const res = await fetch('https://api.example.com/data')
    data.value = await res.json()
  } catch {
    error.value = 'Failed to load'
  }
})
&lt;/script&gt;
Attempts:
2 left
💡 Hint

Look for code that checks the HTTP response status and catches errors properly.

state_output
advanced
2:00remaining
What is the final state of 'items' after this API call in Vue?

Consider this Vue code fetching a list of items and filtering them. What will be the value of items.value after the API call?

Vue
<script setup>
import { ref, onMounted } from 'vue'
const items = ref([])
onMounted(async () => {
  const res = await fetch('https://api.example.com/items')
  const data = await res.json()
  items.value = data.filter(item => item.active)
})
</script>
A[{id: 1, active: true}, {id: 3, active: true}] (only active items from API data)
B[] (empty array because filter is not applied correctly)
C[{id: 1, active: true}, {id: 2, active: false}, {id: 3, active: true}] (all items from API)
Dnull (items is never assigned)
Attempts:
2 left
💡 Hint

Think about what filter does to the array.

🔧 Debug
expert
2:00remaining
Why does this Vue API fetch cause a runtime error?

Examine this Vue code snippet. It tries to fetch user data but crashes with 'TypeError: Cannot read property 'name' of null'. Why?

Vue
<script setup>
import { ref, onMounted } from 'vue'
const user = ref(null)
onMounted(async () => {
  const res = await fetch('https://api.example.com/user/1')
  user.value = await res.json()
})
</script>
<template>
  <div>
    <p v-if="user">Name: {{ user.name }}</p>
  </div>
</template>
AThe template tries to access 'user.name' before 'user' is set, causing an error because 'user' is initially null.
BThe fetch URL is incorrect, so the API returns an error causing the crash.
CThe 'ref' is not imported properly, so 'user' is undefined.
DThe 'onMounted' hook is not supported in Vue 3, causing the code to fail.
Attempts:
2 left
💡 Hint

Check what happens before the API call finishes and how the template uses 'user'.