K
K
krispeyJAk2022-02-25 12:55:47
Vue.js
krispeyJAk, 2022-02-25 12:55:47

Vue how to properly use v-for and v-if together?

There is a layout:

<template v-if="!model.isFreeTime">
          <el-checkbox-group v-model="model.times">
            <el-checkbox-button :label="item.value" :key="item.value" :disabled="item.disabled" v-if="this.model.selectedTime == item.extraDate">{{item.label}}</el-checkbox-button>
          </el-checkbox-group>
        </template>

How to "make friends" v-for and v-if here?
v-if checks two values ​​from the model, and if they are equal, then the element is displayed
v-for iterates over the array and displays values ​​from it

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
InfernoElegy, 2022-02-25
@InfernoElegy

As an option not to create a new html element

<template v-if="">
  <div v-for="" />
</template>

A
Alexander, 2022-02-25
@Aleksandr-JS-Developer

correctly "make friends" v-for and v-if

To make v-for and v-if friends, civilized people usually first filter the array, and then give it to render.

M
Michael, 2022-02-25
@Akela_wolf

The vue documentation answers .
In short: do not hang these two elements on one element, spread them to different levels.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question