Skip to content

An actually one root template is warned should contain exactly one root element #4128

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

Closed
plantain-00 opened this issue Nov 5, 2016 · 6 comments

Comments

@plantain-00
Copy link

Vue.js version

2.0.3

Reproduction Link

<div v-if="type===1"></div>
<div v-if="type===2"></div>
<div v-if="type===3"></div>
<div v-if="type===4"></div>
<div v-if="type===5"></div>

Steps to reproduce

What is Expected?

No warning, because it will generate at most one element.

What is actually happening?

Warning: Component template should contain exactly one root element

@anishdcruz
Copy link

This is not a bug,

@plantain-00 wrap the content within a div.

<div>
  <div v-if="type===1"></div>
  <div v-if="type===2"></div>
  <div v-if="type===3"></div>
  <div v-if="type===4"></div>
  <div v-if="type===5"></div>
</div>

@plantain-00
Copy link
Author

@anishdcruz
I'm OK with closing this issue, if it is difficult.

Angular2 has one root rule too, but it doesn't ban:

<div *ngIf="type===1"></div>
<div *ngIf="type===2"></div>
<div *ngIf="type===3"></div>
<div *ngIf="type===4"></div>
<div *ngIf="type===5"></div>

reactjs can do it by other way like:

if (this.props.type === 1) {
  return <div>...</div>
}
if (this.props.type === 2) {
  return <div>...</div>
}
...

@anishdcruz
Copy link

@plantain-00
Copy link
Author

#3878 issued a similar situation, and it is fixed now.
anyway, if it is difficult, or not considered as a bug, feel free to close this.

@dsonet
Copy link
Contributor

dsonet commented Nov 5, 2016

In this case, since there is no v-else, what if all v-if conditions are false?

@yyx990803
Copy link
Member

The problem here is that without code analysis of all your v-if expressions (in fact it's near impossible because it depends on your component's runtime state), there's no way to tell whether it will really return only one element.

You can get around this by either using a dynamic component (turning all conditional branches into components), or use a render function instead.

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

No branches or pull requests

4 participants