Search code examples
vuejs2slot

get list of element with slot attribute vue


i have in my parent a custom tag <datatable> with slot references in it:

 <datatable> 
        <div slot="week"> 5</div>
        <div slot="day"> monday</div>
    </datatable>

Is it possible to get an array of the slot names?

tried: document.querySelectorAll('[slot]');

But all i get is an empty nodelist.


Solution

  • You can get the array like this (https://v2.vuejs.org/v2/api/#vm-slots):

     <datatable ref="comp"> 
            <div slot="week"> 5</div>
            <div slot="day"> monday</div>
        </datatable>
    
    console.log(Object.keys(this.$refs.comp.$slots));