Let's create the pages that need to have the data rendered in the following steps:
- Create an index user page to list all users:
// pages/users/index.vue
<li v-for="user in users" v-bind:key="user.id">
<nuxt-link :to="'users/' + user.id">
{{ user.name }}
</nuxt-link>
</li>
<script>
import axios from '~/plugins/axios-api'
export default {
async asyncData({error}) {
try {
let { data } = await axios.get('/api/users')
return { users: data.data }
} catch (e) {
// handle error
}
}
}
</script>
On this page, we use the get method from Axios to call the API endpoint of /api/users, which will be transformed to localhost:3000/api/users, where the list of users can be output as follows:
{"status":200,"data":[{"id":1,"name":"Alexandre"},{"id":2,"name":"...