question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

conditional root template in .vue file

See original GitHub issue

Version

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:closed
  • Created 4 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
sirlancelotcommented, Mar 12, 2020
<template v-if="true">
  <p>test</p>
</template>
<template v-else>
  <p>test2</p>
</template>

Should be:

<template>
  <p v-if="true">test</p>
  <p v-else>test2</p>
</template>

Vue single file components should always have no more than one root <template> tag.

0reactions
derMartcommented, Mar 12, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found