Within my child component, I am facing an issue where I need to access the parent object but the commented lines are not functioning as expected.
The structure of AccordionState is defined below:
export type AccordionKeys = | "open" | "disabled";
export type AccordionState = {
[key: string]: {[key in AccordionKeys]: boolean; };
}
Here is the code snippet from the child component:
<script lang="ts">
import { AccordionState } from "@/types/global";
import { defineComponent, useAttrs } from "vue";
export default defineComponent({
name: "AccordionPane",
props: {
panelName: {
type: String,
required: true,
},
},
computed: {
open(): boolean {
// const attrs = useAttrs();
// const state = attrs.state as AccordionState;
// return state[this.panelName].open && !state[this.panelName].disabled;
return true;
},
disabled(): boolean {
// const attrs = useAttrs();
// const state = attrs.state as AccordionState;
// return state[this.panelName].disabled;
return false;
},
In the parent component. The implementation of computed property getProps verifies that prop.state is being defined correctly, However, the commented section in AccordionPane results in a null value for variable "state". Due to limited TypeScript examples online and unstructured JavaScript examples, I'm finding it challenging to adapt them to TypeScript.
<template>
<div class="accordionControl">
<slot ref="content"></slot>
</div>
<textarea class="debug" v-model="getProps"></textarea>
</template>
<script lang="ts">
import { type AccordionState } from '@/types/global';
import { defineComponent, type PropType } from 'vue';
export default defineComponent({
name: "AccordionControl",
props: {
state: {
type: Object as PropType<AccordionState>,
required: true,
},
isVert: {
type: Boolean,
default: false,
},
allOpen: {
type: Boolean,
default: false,
}
},
computed: {
getProps(): string {
return JSON.stringify(this.$props, null, ' ');
}
}
});
</script>
Check out the working example provided in this JavaScript link: