Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
397 views
in Technique[技术] by (71.8m points)

list - How to not show all items in v-for

I have a list like this:

data:{
        slides:[
            { slideImg: "images/necklace.webp"},
            { slideImg: "images/necklace2.webp"},
            { slideImg: "images/pants.webp"},
            { slideImg: "images/pants2.jpeg"},    
        ],    
    }

I try with v-for:

<div class="slide" v-for="slide in slides">
   <img :src="slide.slideImg">       
</div>

V-for makes all items show. But i have an option list, example: when I click option "1 per page" then 1 slide will be shown, "2 per page" then 2 slides will be shown, then 3, 4, 5... Can someone show me how to do that?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Best practice is to make a computed value which filters the items you want to show.

computed: {
  itemsToShow() {
    return this.items.slice(0, this.itemsToShowCount);
  }
}

then use v-for on this computed item.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...