When dealing with a specific use-case, it becomes necessary to retrieve the rendered width
and height
of a DOM element inside a slot. Typically, this can be achieved by accessing this.$slots.default[0].elm
.
However, complications arise when introducing scoped slots to access component data, resulting in this.$slots
becoming empty and causing code errors.
The question remains: How can one access the DOM element of a slot that utilizes a slot-scope?
This issue can be illustrated through the following basic example code:
App.vue
:
<template>
<div id="app">
<HelloWorld v-slot="{ someBool }">
<p>{{ someBool }}</p>
<h1>Hello</h1>
</HelloWorld>
<HelloWorld>
<h1>Hello</h1>
</HelloWorld>
</div>
</template>
<script>
import HelloWorld from './components/HelloWorld.vue';
export default {
name: 'App',
components: {
HelloWorld,
},
};
</script>
HelloWorld.vue
:
<template>
<div class="hello">
<slot :someBool="someBool" />
</div>
</template>
<script>
export default {
name: 'HelloWorld',
data() {
return {
someBool: false,
};
},
mounted() {
console.log(this.$slots);
},
};
</script>