Vue: Add "keep-children" argument to v-if

Created on 18 Jun 2018  路  8Comments  路  Source: vuejs/vue

What problem does this feature solve?

Imagine you have this component for wrapping an input:

<template>
  <div class="container">
    <input />
  </div>
</template>

Like in this case I have several others where I would like to make only the container enclosing tag conditionally visible, but keeping the input, so it would be:

<template>
    <input />
</template>

... depending on certain condition.

I know it's possible to achieve this using the render function, but it's too hard for me to maintain those (big in my case) components without really viewing the template as HTML itself.

What does the proposed API look like?

So, my suggestion is to add an argument to the v-if directive like "keep-children". This way I could do something like:

<template>
  <div v-if:keep-children="showContainer" class="container">
    <input />
  </div>
</template>

<script>
export default {
  props: ['showContainer']
}
</script>

Most helpful comment

You can achieve it by creating really simple functional component https://jsfiddle.net/hpeocn97/

All 8 comments

In the very simple example above I could just:

<template>
  <div v-if="showContainer" class="container">
    <input />
  </div>
  <input v-else />
</template>

but it's too verbose and imagining a big content inside the container all the code would be duplicated

Yes currently we can only use render function to implement a conditional wrapper with a lot of content.

Instead of introducing a modifier for v-if, maybe I would rather use something like:

<template>
  <div v-transparent="!showContainer" class="container">
    <input />
  </div>
</template>

or v-omit

You can achieve it by creating really simple functional component https://jsfiddle.net/hpeocn97/

As @sqal said this is feasible in userland with a functional component

googd

I tried to apply this to my component that is not functional and I didn't achieve the desired result because I have multiple elements inside the container and the render function can't return multiple root node elements and I had to add all them into a div.
https://jsfiddle.net/hpeocn97/14/

You can with a functional component, check this jsfiddle by @Akryum https://jsfiddle.net/Akryum/1ow9s73y/

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cherry-geqi picture cherry-geqi  路  35Comments

yyx990803 picture yyx990803  路  48Comments

karevn picture karevn  路  36Comments

yyx990803 picture yyx990803  路  210Comments

eu81273 picture eu81273  路  40Comments