How get component instance from unnamed slots on Vue.js2
How get component instance from unnamed slots on Vue.js2
I have next template:
<TableComponent>
<TableColumn v-bind:field="'firstName'" v-bind:label="'First Name">
</TableColumn>
How i can get component instances of TableColumn
?
TableColumn
On TableComponent I have next mounted
method:
mounted
mounted () {
const columnComponents = this.$slots.default
.filter(column => column.componentInstance)
.map(column => column.componentInstance)
console.info(columnComponents)
},
But on console info I got empty array. How I can get TableColumn
props?
TableColumn
1 Answer
1
Use a ref
ref
<TableComponent>
<TableColumn ref="tableColumn" v-bind:field="'firstName'" v-bind:label="'First Name">
</TableColumn>
and then
mounted() {
this.$nextTick(() => {
const col = this.$refs.tableColumn;
});
}
undefined
Maybe
TableColumn
not mounted?– withoutname
Jun 29 at 17:32
TableColumn
If the child isn't mounted, you can use
.nextTick
Updating answer now– Stephen Thomas
Jun 29 at 17:35
.nextTick
How I can mount child component? Without use
.nextTick
?– withoutname
Jun 29 at 18:08
.nextTick
and this solution is not working too
– withoutname
Jun 29 at 21:44
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
It returns
undefined
.– withoutname
Jun 29 at 17:20