Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue.js "$slots.default()[0].el" is null when passing dynamic slot with v-for

I'm trying to access HTMLElement of dynamic slot that passed by v-for but el is always empty

app.vue :

<template>
  <div id="app">
    <prod>
       <span>
         <div  v-for="item in items">{{item.title}}</div>
       </span>
    </prod>
  </div>
</template>
.
.
.
data: () => {
    return { items: [{ title: '1' }, { title: '2' }] };
  }

prod.vue :

<template>
  <slot></slot>
</template>
.
.

  mounted() {
    
       console.log(this.$slots.default()[0].el);
  }
.
.

but el is always null but when I pass it hardcoded like below el is available

<prod>
   <span>
     <div>a</div><div>b</div>
   </span>
</prod>

I mean how I can access HTMLElement of dynamic slot ?

like image 795
user4254398 Avatar asked Dec 07 '25 07:12

user4254398


1 Answers

its look like there is no solution for this But we can use refs

<div ref="main">
  <slot></slot>
</div>
.
.
// and we can access the dom with :
this.$refs.main 

also there is updated method that you can monitor changes of slots

like image 87
user4254398 Avatar answered Dec 08 '25 19:12

user4254398



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!