2

I would like to save the latest index inside a v-for, but I don't know how to include this logic inside the template?

Basically, I have this:

<div v-for="(reply, index) in replies" :key="reply.id">

I want to include this logic inside the template somehow:

this.last_index = index

I just want to know the amount of total replies in that v-for loop.

Thank you!

1
  • 4
    replies.length gives the total number of replies. Commented Jul 31, 2017 at 10:04

1 Answer 1

1

The most adequate solution is use a computed property in Vue. I usually made it like this:

computed: {
  
  lengthReply() {
  
      return this.replies.length;
      
  }
  
}

If you use Vuex, then you probably can use getters for this case. If you really don't wanna use computed property, then you can put it in your template in curly braces, like this:

{{ replies.length }}

If you need to render only last element in your reply list, then you can use this terrible code:

<div v-for="(reply, index) in replies" v-if="reply === replies[replies.length - 1]">
Sign up to request clarification or add additional context in comments.

1 Comment

Why not just do v-if="index === (replies.length - 1)"?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.