Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vuejs - <slot> in repeating table row

im using this solution to set table cells in a vuejs component dynamically:

http://forum.vuejs.org/topic/526/repeating-table-row-with-slot

This works just with Vue.js v1.0.10 but not with the current version v1.0.26.

Jsfiddle: https://jsfiddle.net/peL8fuz3/

I'm trying to get the following markup (the item object should be available in the component)

<div id="vue">
    <basic-table>
        <table-cell>{{ item.id }}</table-cell>
        <table-cell>{{ item.title }}</table-cell>
    </basic-table>
</div>

Vue.js component (more at the fiddle)

Vue.component('basic-table', {
    template: '<table><tbody><tr v-for="item in collection" v-slot></tr></tbody></table>',
    data: function () {
        return {
            collection: [
                {id: 1, title: 'Vue'},
                {id: 2, title: 'Vue 2'},
                {id: 3, title: 'Vue 3'},
                {id: 4, title: 'Vue 4'},
            ]
        }
    }
});

Anyone knows how to solve this?

Edit Didnt found a working solution yet - still searching..

like image 792
Aaroniker Avatar asked Aug 16 '16 08:08

Aaroniker


People also ask

How do you pass a Vue slot?

Scoped slots To pass a property in Vue, you would define the data in the parent component and assign a value to the data. Then, you'd pass the value of the property to the child component so the data becomes a property in the child component.

What is Vslot?

v-slot is used on an HTML element. v-slot can only be used on a <template> element or the component receiving the slot. Otherwise, Vue throws a compile error. See Example 1 below. v-slot is used on a component, but the component has another <template v-slot> as a child.

How do you make a Vue slot?

So in the most basic sense when you want to use slots in VueJS, you add the <slot></slot> tags in the child component where you expect to receive data from outside. Slot is a reserved keyword in VueJs and as soon as it sees that markup, it does it's thing to render the content that is being passed from the outside.

What scoped slot?

Scoped slots are exactly like regular slots, with the difference that we pass data from the child component to the parent component. This data can then be used inside the slot content.


1 Answers

That's quite hard to find out what exactly gone wrong, but that code was broken since v1.0.18. I was unable to dig deeper to eliminate exact commit but there was a couple of optimizations made which potentially could affect $context._content rendering.

Solution

However you can solve your problem by changing

var raw = host.$options._content; 

to

var raw = host._context._directives.filter(function (value) {
    return !(value.Component === undefined);
})[0].el;

That change is compatible with v1.0.26. You can find fixed code here.

Disclaimer

I was failed to find a way to achieve the same result using public API. So this solution is also relies on non-public API thus may break in future release.

like image 161
vsminkov Avatar answered Sep 27 '22 17:09

vsminkov