vuejs / vue

This is the repo for Vue 2. For Vue 3, go to https://github.com/vuejs/core
http://v2.vuejs.org
MIT License
207.66k stars 33.66k forks source link

Suggestion: "$last", "$even", "$odd" etc (v-for special properties) #1842

Closed furey closed 8 years ago

furey commented 8 years ago

Would it be relatively inexpensive to add other special properties to v-for loops?

For example (from the Angular docs):

ngrepeat

I'm currently writing a breadcrumb component and a property like $last would simplify:

<li :class="{ active: $index + 1 == crumbs.length }">

into:

<li :class="{ active: $last }">

Interested to hear your thoughts!

yyx990803 commented 8 years ago

Unfortunately, it is in fact relatively expensive to add these aliases, because in Vue these properties need to be reactive, so each of them would have extra initialization and memory costs. It would be wasteful to always define them on every repeated fragment since they are not always used.

You can define custom methods like isLast and just use it like

<li :class="{ active: isLast($index) }">
furey commented 8 years ago

Understood. Cheers.