Say I want to create a multi layered Component for reuse like a 'Tab' Ui.
So the using developer could write:
<tabs>
<tab label="My First Tab">
Content for first tab which could contain components, html, etc.
</tab>
<tab label="My Second Tab">
More Content
</tab>
</tabs>
Basically, they would use the Tabs and Tab components in such a way that I have no idea how many Tab components the dev will use in the Tabs. How do I access the Tab components to, for example, show and hide them per tab UI functionality?
I've tried using this.$children and this.slots.default but was not actually able to access the Tab data to show and hide it. To be fair, I'm writing in Typescript and the issue may be more difficult because of it.
Something like:
<template>
<div class="tabs">
<slot /> <!-- Location of the Tab's -->
</div>
</template>
<script lang="ts">
import { Component, Vue } from "vue-property-decorator";
@Component({
...
})
export default class Tabs extends Vue {
public tabs: any[] = [];
public created() {
this.tabs = this.$slots.default;
// this.tabs is a set of VNode's,
// I can't figure out how to access the components
// I see a prop 'componentInstance' but when I try to
// access it, it says undefined
// this.select(0);
let tab = this.tabs.find((obj: any, index: number) => {
return index === 0;
}); // tab is undefined :(
}
public select(index: number) {
(this.$slots.default[index] as any).show = true;
// Accessing Vnode instead of Component :(
// this.$children[0]["show"] = true; // Read only :(
// this.tabs[index].show = true; // blerrggg :(
// Vue.nextTick();
}
}
</script>
I've looked at some GitHub libs for Tabs in Vue and the logic seems very complicated. I'm assuming from the existence of slots/children there is a more straight-forward approach to accessing child components. Maybe that's overly hopeful on my part.
Anyone know how to access, pass or change data in a child slot if you don't know how many children will be there (ie you didn't explicitly write them in the parent Component)?