I'm experimenting with Nuxt for building SPA's and static sites, and while I understand the concept of grabbing data from an API and looping through it in Vue on the page itself, I'm struggling with how to do this with a component. In my example I am trying to grab posts from an API, and simply list all the posts with their type and title. I'd like to get more sophisticated later, but want to understand the basic concept first. How can I get the "posts" data from the index.vue.
My index.vue file:
<template>
<div>
<navbar />
<logo />
<ul>
<list /> // want the posts to be looped in this component
</ul>
</div>
</template>
<script>
import Navbar from '~/components/Navbar.vue'
import Logo from '~/components/Logo.vue'
import List from '~/components/List.vue'
export default {
components: {
Navbar,
Logo,
List
},
async asyncData ({ $axios }) {
const posts = await $axios.$get('https://www.myapi.com/posts')
return {
posts
}
}
}
</script>
<style>
</style>
Here is my List component:
<template>
<li v-for="post in posts">
{{ post.type }} - {{ post.title }}
</li>
</template>
<script>
// do i need props here??
</script>