Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can v-for have local variables? #12088

Closed
hezedu opened this issue May 22, 2021 · 4 comments
Closed

Can v-for have local variables? #12088

hezedu opened this issue May 22, 2021 · 4 comments

Comments

@hezedu
Copy link

hezedu commented May 22, 2021

What problem does this feature solve?

Repeated comparisons: v.id === currentId

<ul>
  <li v-for="v in list"
    :class="v.id === currentId ? 'active' : ''">
    {{v.id === currentId ? 'active' : 'not active'}}
  </li>
</ul>

What does the proposed API look like?

Define a local variable: isActive

<ul>
  <li v-for="v in list, isActive = v.id === currentId"
    :class="isActive ? 'active' : ''">
    {{isActive ? 'active' : 'not active'}}
  </li>
</ul>
@Killea
Copy link

Killea commented May 25, 2021

it could be this?

<ul>
  <template v-for="v in list">
    <li v-if="v.id === currentId" class="active">
    active
    </li>
    <li v-else>
     not active
    </li>
  </template>
</ul>

@hezedu
Copy link
Author

hezedu commented May 25, 2021

@Killea No, I don't want to repeat. <li> will be more other same attributes, such as key.


And template can't set key

@posva
Copy link
Member

posva commented May 27, 2021

Please search v-scope in issues and PR. This was dropped

@posva posva closed this as completed May 27, 2021
@zetaraku
Copy link

There is a trick to achieve it, but I think an official support will be nice.

<ul>
  <template v-for="v in list">
    <template v-for="isActive in [v.id === currentId]">
      <li :class="{ 'active': isActive }">
        {{ isActive ? 'active' : 'not active' }}
      </li>
    </template>
  </template>
</ul>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants