I have trouble figuring out how to get the following to work:
My parent template
<comp>
<a href="#" slot="links>link 1</a>
<a href="#" slot="links>link 2</a>
</comp>
and my component comp
template looks like the following:
<ul class="comp">
<li class="comp-item"><slot name="links"></slot></li>
</ul>
currently all my anchors goes to that single li
tag (which is expected)
but I would like to be able to produce multiple li
for every named slot I inserted like the following:
<ul class="comp">
<li class="comp-item"><a href="#" slot="links>link 1</a></li>
<li class="comp-item"><a href="#" slot="links>link 2</a></li>
</ul>
Is there any way to achieve what I need without using scoped slot? Because my content is pure HTML so I feel it is unnecessary to put static content inside prop in order to render them.
From what I have seen, most vue UI framework requires you to use another custom component for the list item, which I feel is over killed for the problem. Is there any other way to do this?
If you don't like to put your data in array, and render list with
v-for
You can put all of them in the component, no slot:
or with slot:
This is easily accomplished with a render function.
Here is a working example.
Or with the help of a small utility component for rendering vNodes you could do it like this with a template.
You can make use of scoped slots instead of slots
Your
comp
component receives a proplinks
which is an array of links(since static initialized as a custom option). Iterate over thelinks
and passlink
as data to the slot just like passing props to a componentIn the parent where the
comp
component is used make use of a<template>
element with a special attributeslot-scope
, indicating that it is a template for a scoped slot.The value of
slot-scope
will be used as the name of a temporary variable that holds the props object passed from the child:Here is the working fiddle