M
M
MikUrrey2021-04-15 12:48:31
Vue.js
MikUrrey, 2021-04-15 12:48:31

How to pass the contents of a named slot to a child in a render function?

1) There is a component that, using the render function, displays another component:

export default {
   name: "form-element",
   render(h) {
      let data = {
          //.....................................
      };
      //.....................................
      return h(component, data);
   }
}

2) The rendered component has a named slot `prepend-inner`, how can I pass its contents through `form-element`?
<form-element key="site">
   <template slot="prepend-inner">
      <img :src="`${form.site}/favicon.ico`">
   </template> 
</form-element>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MikUrrey, 2021-04-15
@MikUrrey

Everything turned out to be simple, I share the solution.
You need to pass static slots as children (the third argument to createElement), and put the corresponding slot in the data of each child:

export default {
   name: "form-element",
   render(h) {
      let data = {
          //.....................................
      };
      //.....................................
      let slots = []
      for(let slot in this.$slots) {
        slots.push(h('template', {slot}, this.$slots[slot]))
      }
      return h(component, data, slots);
   }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question