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

Vue Fragments ( React Fragments ) #7606

Closed
ralphchristianeclipse opened this issue Feb 6, 2018 · 4 comments
Closed

Vue Fragments ( React Fragments ) #7606

ralphchristianeclipse opened this issue Feb 6, 2018 · 4 comments

Comments

@ralphchristianeclipse
Copy link

ralphchristianeclipse commented Feb 6, 2018

What problem does this feature solve?

This solve the issue where multiple root components is not allowed like for instance

i'm trying to do this with slots using my own component for page loading and checking if its on loading and not loading state and checking if its valid to render the main component

<template lang="pug">
  v-progress-linear.ma-0(v-if="loading" color="secondary")
  div(v-else-if="loaded")
    slot
  v-container(fluid fill-height v-else)
    v-layout(justify-center align-center)
      slot(name="no-result")
        h1.text-xs-center No Result
</template>

<script>
export default {
  name: "Page",
  props: {
    loading: {
      type: Number,
      default: 0
    },
    valid: {
      type: Boolean
    }
  },
  computed: {
    loaded() {
      return this.valid && !this.loading;
    }
  }
};
</script>

So in this part of the template makes another div to just prevent the multiple root issue

  div(v-else-if="loaded")
    slot

so doing it like this would help

slot(fragment) Fall back content

I know this thing can be done somehow on render functions

but its not vueish anymore :)

What does the proposed API look like?

so in slots

we can do

<slot fragment></slot>

and in template syntax

<template fragment> </template>

Got the idea here https://reactjs.org/docs/fragments.html

@jkzing
Copy link
Member

jkzing commented Feb 6, 2018

Actually you can add v-else-if directly on slot like:

slot(v-else-if="loaded")

Besides, template is already working like React Fragment I think.😃

@ralphchristianeclipse
Copy link
Author

@jzking it emits errors

Cannot use as component root element because it may contain multiple nodes.

@posva
Copy link
Member

posva commented Feb 6, 2018

Duplicate of #7088

@posva posva marked this as a duplicate of #7088 Feb 6, 2018
@posva posva closed this as completed Feb 6, 2018
@deot
Copy link

deot commented Oct 19, 2018

  • fragment
export default {
    functional: true,
    render(h, ctx) {
        return ctx.children;
    }
};
  • table
<template>
    <table>
        <thead>
            <th v-for="item in columns" :key="item">
                {{ item }}
            </th>
        </thead>
        <tbody>
            <template v-for="item in data">
                <slot v-bind="item" />
            </template>
        </tbody>
    </table>
</template>

<script>
// ...
</script>

use it like this:

<template>
    <vc-table>
        <!-- use it by slot-->
        <vc-fragment slot-scope="it" :key="it.id">
            <tr @click="handleClick($event, it.id)">点我</tr>
            <tr>2</tr>
            <tr>3</tr>
            <tr>4</tr>
            <tr>5</tr>
        </vc-fragment>
    </vc-table>
</template>
<script>
import Table from './Table';
import Fragment from './fragment';

export default {
    name: 'vc-fragment-basic',
    components: {
        'vc-table': Table,
        'vc-fragment': Fragment
    },
    methods: {
        handleClick(e, id) {
            alert('你点击了我');
        }
    }
};
</script>

u can't do that like this:

<template>
    <vc-fragment>
        <tr>1</tr>
        <tr>2</tr>
        <tr>3</tr>
        <tr>4</tr>
        <tr>5</tr>
    </vc-fragment>
</template>

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

4 participants