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

"Multiple" root components could be allowed if v-if and v-else are used #3878

Closed
aphofstede opened this issue Oct 8, 2016 · 3 comments
Closed

Comments

@aphofstede
Copy link

Vue.js version

2.0.1

Reproduction Link

https://jsfiddle.net/alexman/h64y6bj1/1/

Steps to reproduce

Set up a standalone component with template:

<template>
    <div v-if="isComplex"><slot></slot></div>
    <button v-else><slot></slot></button>
</template>

And computed field:

  computed: {
    isComplex() {
            return false // true
        }
  }

What is Expected?

v-if with a v-else following it resolves to exactly 1 component, so I believe it could be allowed and not throw an error about a missing root component.
Making isComplex() return true in the example should display a div, and false would display a button.

What is actually happening?

Component template should contain exactly one root element:

<div v-if="isComplex"><slot></slot></div>
<button v-else><slot></slot></button>
@aphofstede
Copy link
Author

My work-around for now:

<template>
    <component is="button-view"><slot></slot></component>
</template>

<script>
  export default {
    components: {
      'button-view': Vue.component('button-view', {
        render: function (createElement) {
          return createElement(
                  this.$parent.isComplex ? 'button' : 'div',
                  this.$slots.default
          )
        }
      })
    },
    computed: {
      isComplex() {
        return true // false
      }
    }
  }
</script>

Does that make sense?

@posva
Copy link
Member

posva commented Oct 9, 2016

wasn't this added already @chrisvfritz ?
@aphofstede Using jsx would also work but it doesn't fit all use cases as well as template does.

@chrisvfritz
Copy link
Contributor

chrisvfritz commented Oct 9, 2016

I do distinctly remember adding this feature. I'll investigate, probably later today.

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

3 participants