conditional root template in .vue file
See original GitHub issueVersion
2.6.11
Reproduction link
https://codesandbox.io/s/sad-benz-d69xb
Steps to reproduce
Not sure if this applies to vue or vue-loader. It seems v-if statements are not processed correctly when inside a root template tag. Suppose you have the following .vue file:
<template v-if="true">
<p>test</p>
</template>
<template v-else>
<p>test2</p>
</template>
What is expected?
The test paragraph should be rendered as v-if is true (also tested with other more complicated truthy conditions)
What is actually happening?
The test2 paragraph is rendered. It seems always the last template tag, having either v-else or any v-else-if statement is rendered, even if that v-else-if condition is false.
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
vuejs template conditional root element - Stack Overflow
I'm making a generic vue button component. In some cases it would be a router-link while in other cases it would be a...
Read more >Conditional Rendering - Vue.js
In this case we can use v-if on a <template> element, which serves as an invisible wrapper. The final rendered result will not...
Read more >Vue conditional rendering: editing existing todos
In your components directory, create a new file called ToDoItemEditForm.vue . Copy the following code into that file: <template> <form ...
Read more >Using v-if to render one template or another based on a props
Using v-if to render one template or another based on a props. Hey lovely people,. I'm slowly easing myself into understanding and using...
Read more >How can I use conditional Template tags inside a Vue SFC ...
In vue2 with regular .vue files you must have a single root. So you could accomplish this by wrapping the entire thing in...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Should be:
Vue single file components should always have no more than one root
<template>
tag.Thank you very much. I know of the single root requirement, but the problem above is exactly of the nature you describe and as such does not violate that requirement. Every branch resolves to a single component, even to the same element (p tag), but still the conditions are not resolved correctly. Here is finally the example on codesandbox: https://codesandbox.io/s/sad-benz-d69xb There, as stated above test2 is rendered instead of test So if this should be supported, I guess it is a bug you might consider to reopen. If not, please update the documentation and please generate a warning while compiling / rendering the component.