Passing slots through from Parent to Child Components

Edgar Navasardyan

I have built a user-defined component (async-select) on top of another component (vue mutliselect) like this:

https://jsfiddle.net/2x7n4rL6/4/

Since the original vue-multiselect component offers a couple of slots, I don't want to loose the chance to use them. So my goal is to make these slots available from inside my custom component. In other words, I want to something like this:

https://jsfiddle.net/2x7n4rL6/3/

But that code oes not work. However, if I add the slot to the child component itself, it works just fine (which you can see from the fact that options become red-colored).

https://jsfiddle.net/2x7n4rL6/1/

After surfing the web, I have come across this article, but it does not seem to work Is there any way in VueJS to accomplish this ?

Brother Woodrow

Slots can be confusing!

First, you need a template element to define the slot content:

  <async-select :value="value" :options="options">
    <template v-slot:option-tmpl="{ props }">
      <div class="ui grid">
        <div style="color: red">{{ props.option.name }}</div>
      </div>
    </template>
  </async-select>

Then, in the parent component, you need a slot element. That slot element itself can be inside of another template element, so its contents can be put in a slot of its own parent.

<multiselect
    label="name"
    ref="multiselect"
    v-model="localValue"
    placeholder="My component"
    :options="options"
    :multiple="false"
    :taggable="false">
        <template slot="option" slot-scope="props">
            <slot name="option-tmpl" :props="props"></slot>
        </template>
</multiselect>

Working Fiddle: https://jsfiddle.net/thebluenile/ph0s1jda/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Vuejs passing function from parent to child

How to pass props using slots from parent to child -vuejs

Passing data from service to Component --> Child Components

How to define placeholders/slots in grand parent and inject component from child?

Angular passing data from parent to child is undefined

Passing data from Parent to Child via template

Passing prop from child to parent is null

Passing button click from child to parent in React

React: Passing state from child to child to parent

Angular Architecture: Passing Data to Child from Parent

Accessing child refs without passing it from parent

How to pass onClick handler from parent to child components through optional labeled arguments

Passing a prop to a child from parent with React and Gatsby

ReactJS Passing state from parent to child

Passing data child to parent functional components in reactjs

ReactJS: Issues when passing data from parent to child component right after fetching data from API. (functional components only)

Passing data from parent to child (Nested Form)

Angular2 passing data between NON parent - child components

React Redux passing parameters via functions from child components to parent

Passing array from parent to child

Passing state from parent to child route

Passing props through multiple components (four), parent State not updating

Passing props from parent component to child components

passing data from parent class to child class through props, but props not returning anything in child class

Passing state from parent to child to child

How to modifying child component's slots from parent components in Svelte

passing function between separate components in react not between child and parent

React.JS - Passing Data Via Components From Child to Parent

Passing data from child to parent, with an init in the child

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive