Please review my code.
<template>
<div class="row flex">
<div class="col-md-6 home_feed">
<post-detail :posts="posts"></post-detail>
</div>
</div>
</template>
<script>
import axios from 'axios'
export default {
async asyncData (params) {
let { data } = await axios.get('http://localhost:8000/api/v1/users/' + this.$route.params.id + '/')
return {
posts: data
}
},
components: {
'post-detail': Item
}
}
</script>
I got this error: Cannot read property '$route' of undefined when I asyncdata from params.id, but when I type: console.log(this.$route.params.id), it goes right. How can I fix this
console.log(this)inside your export? I'm pretty sure you will need vue instance to access the routethisinsideasyncData. By the looks of thenuxtdocumentation (I've never used it) you need to pass in a context to get a route param:asyncData({params, context})then you can access it as:context.params.id.