Answer the question
In order to leave comments, you need to log in
Why is some data undefined when navigating to the page?
UPD: I solved the issue by simply adding the data itself to the v-if check. Everything began to work as it should, in fact, why is it necessary to do so? Some crutch.
Decision:
<div class="comments_items_title">{{card.comments && card.comments.length}} комментариев </div>
<div class="comments_items" v-if="card.comments && card.comments.length">
<Comment v-for="comment in card.comments"
:key="comment._id"
:comment="comment"
/>
</div>
<div v-else>Комментариев нет</div>
<CommentForm
:id="card && card._id"
@created="createCommentHandler"
/>
<template>
<div>
<component :is="res"/>
</div>
</template>
<script>
export default {
async asyncData({store, params}) {
const res = await store.dispatch('data/getData', params.slug)
return {res}
}
}
</script>
<template>
<div>
<h1 class="mb28">{{this.card.title}}</h1>
<footer class="comments">
<div class="comments_items_title">{{card.comments.length}} комментариев </div>
<div class="comments_items" v-if="card.comments && card.comments.length">
<Comment v-for="comment in card.comments"
:key="comment._id"
:comment="comment"
/>
</div>
<div v-else>Комментариев нет</div>
<CommentForm
:id="card._id"
@created="createCommentHandler"
/>
</footer>
</div>
</div>
</template>
<script>
export default {
data () {
return {
card: ''
}
},
async fetch() {
const res = await this.$store.dispatch('card/fetchById', this.$route.params.slug)
await this.$store.dispatch('card/addView', res)
this.card = res
},
methods: {
createCommentHandler(comment) {
this.card.comments.push(comment)
}
}
}
</script>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question