I have Users.vue component in my NuxtJS project. It's look like:
<template>
<section>
<h1>{{ pageTitle }}</h1>
<ul>
<li v-for="user of users" :key="user">
<a href="#" @click.prevent="goTo(user)">User {{ user }}</a>
</li>
</ul>
</section>
</template>
<script>
export default {
asyncData() {
return new Promise(resolve => {
setTimeout(() => {
resolve({
users : [1,2,3,4,5]
})
}, 3000)
})
},
data() {
return {
pageTitle: "Это страница с пользователями"
}
},
methods: {
goTo(user) {
return this.$router.push('/users/' + user)
}
}
}
</script>
When I use promise in asyncData(), I have error [Vue warn]: Property or method "users" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property.
asyncData()is only available in page components. Get the users data inasyncData()of page component first, and pass it to the components inside of page componentfetch()hook.