最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Vue 3: wrap all items within a slot with a custom component by using a render function - Stack Overflow

programmeradmin0浏览0评论

I am trying to build my own sortable ponent. I want to pass a list of items to it's default slot. The sortable ponent should then wrap all passed items with a custom v-draggable ponent.

<v-sortable handle=".handle">
    <template :key="index" v-for="(item, index) in items">
        <some-plex-ponent :item="item"></some-plex-ponent>
    </template>
</v-sortable>

Now withn my v-sortable ponent I am trying to wrap all given nodes within default slot with a custom v-draggable ponent. My v-sortable ponent looks like this:

import { h } from 'vue';

export default {
    name: 'v-sortable',
    props: {
        handle: {
            type: String,
            required: false,
            default: () => {
                return null;
            }
        },
    },
    render () {
        const draggableItems = this.$slots.default().map(slotItem =>
            h('v-draggable', { handle: this.handle }, [slotItem])
        )
        return draggableItems;
    }
}

This works as expected, except that my custom ponent v-draggable will not be rendered as a vue ponent. All items will be wrapped in html tags called <v-draggable>. How would I have to proceed to actually parse the v-draggable ponent as Vue ponent?

I am trying to build my own sortable ponent. I want to pass a list of items to it's default slot. The sortable ponent should then wrap all passed items with a custom v-draggable ponent.

<v-sortable handle=".handle">
    <template :key="index" v-for="(item, index) in items">
        <some-plex-ponent :item="item"></some-plex-ponent>
    </template>
</v-sortable>

Now withn my v-sortable ponent I am trying to wrap all given nodes within default slot with a custom v-draggable ponent. My v-sortable ponent looks like this:

import { h } from 'vue';

export default {
    name: 'v-sortable',
    props: {
        handle: {
            type: String,
            required: false,
            default: () => {
                return null;
            }
        },
    },
    render () {
        const draggableItems = this.$slots.default().map(slotItem =>
            h('v-draggable', { handle: this.handle }, [slotItem])
        )
        return draggableItems;
    }
}

This works as expected, except that my custom ponent v-draggable will not be rendered as a vue ponent. All items will be wrapped in html tags called <v-draggable>. How would I have to proceed to actually parse the v-draggable ponent as Vue ponent?

Share Improve this question edited Aug 17, 2022 at 13:04 Eric Xyz asked Aug 17, 2022 at 11:45 Eric XyzEric Xyz 4921 gold badge6 silver badges16 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

Try to import it and register and use it directly :

import { h } from 'vue';
import VDraggable from 'path/to/v-draggable'
export default {
    name: 'v-sortable',
    props: {
        handle: {
            type: String,
            required: false,
            default: () => {
                return null;
            }
        },
    },
    render () {
        const draggableItems = this.$slots.default().map(slotItem =>
            h(VDraggable, { handle: this.handle }, [slotItem])
        )
        return draggableItems;
    }
}

It's remended to pass items as prop and use them directly inside the render function :

<v-sortable handle=".handle" :items="items">
</v-sortable>

child ponent :

import { h } from 'vue';
import VDraggable from 'path/to/v-draggable'
export default {
    name: 'v-sortable',
    props: {
        items:{
         type:Array,
         default: () =>[]
        },
        handle: {
            type: String,
            required: false,
            default: () => {
                return null;
            }
        },
    },
    render () {
        const draggableItems = this.items.map(slotItem =>
            h(VDraggable, { handle: this.handle }, [item])
        )
        return draggableItems;
    }
}

The ponent can to be explicitly specified in render function:

h(VDraggable, ...)

Globally registered ponent that isn't available for import (e.g. from third-party libs) can be resolved from a name with resolveComponent.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论