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

Allow functional components to have multiple root elements #1168

Open
euoia opened this issue Feb 19, 2018 · 13 comments
Open

Allow functional components to have multiple root elements #1168

euoia opened this issue Feb 19, 2018 · 13 comments

Comments

@euoia
Copy link

euoia commented Feb 19, 2018

What problem does this feature solve?

As discussed in vuejs/vue#7088 multiple root elements are not currently possible in Vue components, with @LinusBorg stating that multiple root elements will not be supported (in the general case) any time soon.

The exception to this is for functional components, where multiple root elements are allowed. Here's a jsfiddle from @trusktr showing it, though using a render function:
https://jsfiddle.net/b049qboe/1/

This feature would allow multiple root elements in elements used in single-file components. Vue-loader 14.1.1 produces the following error in this scenario:

  - Component template should contain exactly one root element. If you are using v-if on multiple elements, use v-else-if to chain them instead.

What does the proposed API look like?

In a single-file component:

<template functional>
    <!-- Multiple root elements, e.g. -->
    <div>lorum</div>
    <div>ipsum</div>
    <div>dolor</div>
</template>

@McSneaky
Copy link

This would make creating functional components a lot easier 🙂

@JoshuaSoileau
Copy link

+1

@atorscho
Copy link

It would be very useful especially in cases when you want to create a component for definition lists.

DefinitionList for <dl> and DefinitionItem for <dt> <dd>.

@stalniy
Copy link

stalniy commented Aug 15, 2018

This issue relates to vue-compiler package and not to vue-loader

@mathroc
Copy link

mathroc commented Aug 15, 2018

@stalniy which package / repository are you talking too? I have no vue-compiler in my vuejs projects

@stalniy
Copy link

stalniy commented Aug 15, 2018

@mathroc I'm saying that the fix should be done in vue template compiler here (https://github.com/vuejs/vue/blob/dev/src/compiler/parser/index.js#L198) and not in vue loader

@nolde
Copy link

nolde commented Jan 11, 2019

Looks like Vue itself already supports multiple roots in functional components.

vuejs/vue#7088 (comment)

@LinusBorg
Copy link
Member

@nolde OP is aware of that, his request us about functional templates.

@nolde
Copy link

nolde commented Jan 13, 2019

@LinusBorg , my point was about @stalniy mentioning that this issue should be fixed in vue-template-compiler instead of vue-loader. If vue already supports that in functional components, it probably supports that in the template compiler already. If it doesn't, then those are two different issues.

@vinaypai
Copy link

+1. Found myself needing this to make effective use of CSS grids (especially while display: contents isn't well supported). I had to work around the limitation by merging what would have been sub-components into a single more complex component.

@nolde
Copy link

nolde commented Jul 11, 2019

Tip for people having this issue: use JSX.

The JSX loaders for vue support multiple roots by returning an array of components/elements, and it also works around the issue of not being able to register local components. It is perfect for functional components.

@vuejs vuejs deleted a comment from oraant Nov 8, 2019
@MLDMoritz
Copy link

I also am facing issues, even in the same case @atorscho, trying to create something like this:

<template functional>
  <dt class="col-sm-3">{{ title }}</dt>
  <dd class="col-sm-9">
    <slot />
  </dd>
</template>

@mryellow
Copy link

https://github.com/vuejs/vue/blob/dev/src/compiler/parser/index.js#L198

@stalniy Note that links without a commit will not match the intended line-number in future.

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