Middle
Call the getProducts function in a Vue 3 component and render the results in the template.
sobes.tech AI
Answer from AI
In Vue 3, to call an asynchronous function getProducts and display the results, you can use the Composition API with setup and reactive state. Example:
<template>
<div>
<ul>
<li v-for="product in products" :key="product.id">
{{ product.name }} - {{ product.price }}
</li>
</ul>
</div>
</template>
<script setup>
import { ref, onMounted } from 'vue'
const products = ref([])
async function getProducts() {
// Here should be a real API call
return [
{ id: 1, name: 'Product 1', price: 100 },
{ id: 2, name: 'Product 2', price: 200 }
]
}
onMounted(async () => {
products.value = await getProducts()
})
</script>
In this example, when the component is mounted, getProducts is called, the result is stored in the reactive variable products, and the data is displayed in a list.