admin管理员组文章数量:1023772
I'm working on a recursive Vue 3 component using the Composition API and the useTemplateRefsList
function from @vueuse/core
.
My goal is to collect references to each instance of the recursive component. However, I'm struggling to type the list of references correctly, as the this
keyword is not available in the Composition API.
Here’s a simplified version of my setup:
// toto.vue
<template>
{{ data.name }}
<toto
v-for="item in data.children"
ref="childRefs"
:key="item.id"
:data="item.children"
/>
</template>
<script setup lang="ts">
import { useTemplateRefsList } from '@vueuse/core';
const { data } = defineProps<{
id: string;
name: string;
children?: [
{
id: string;
name: string,
children?: [...]
}
]
}>();
// Attempting to type the refs list
const childRefs = useTemplateRefsList</* What type goes here? */>();
</script>
Problem
I want to type childRefs
such that it correctly represents an array of the recursive component's instances.
Since this
is unavailable in the script setup syntax, I cannot directly refer to the component's instance type.
What I've tried
- Using
ComponentPublicInstance
:
const refs = useTemplateRefsList<ComponentPublicInstance>();
But this type is too generic and doesn’t reflect the actual instance of the recursive component.
- Using
InstanceType
:
const refs = useTemplateRefsList<InstanceType<typeof RecursiveComponent>>();
However, RecursiveComponent isn’t defined yet during its own declaration, which creates a circular dependency issue.
- Typing via
expose
I considered using defineExpose to expose properties/methods and type against those, but this feels like overkill for a simple recursive component.
- Export type from another
ts file
// definitionsFile.ts
import Toto from './Toto.vue';
// Without using an object, I get the following error: Type alias 'TotoType' circularly references itself.
export type TotoType = { a: InstanceType<typeof PermissionSummaryRow> };
// toto.vue
import Toto from './Toto.vue';
// 'childRefs' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer
const childRefs = useTemplateRefsList<TotoType['a']>();
// any
const test = childRefs.value[0]!;
Question
What is the correct way to type useTemplateRefsList
for a recursive component in Vue 3? Is there a way to dynamically reference the current component's type, or is there a workaround to achieve this?
Any help or guidance would be appreciated!
I'm working on a recursive Vue 3 component using the Composition API and the useTemplateRefsList
function from @vueuse/core
.
My goal is to collect references to each instance of the recursive component. However, I'm struggling to type the list of references correctly, as the this
keyword is not available in the Composition API.
Here’s a simplified version of my setup:
// toto.vue
<template>
{{ data.name }}
<toto
v-for="item in data.children"
ref="childRefs"
:key="item.id"
:data="item.children"
/>
</template>
<script setup lang="ts">
import { useTemplateRefsList } from '@vueuse/core';
const { data } = defineProps<{
id: string;
name: string;
children?: [
{
id: string;
name: string,
children?: [...]
}
]
}>();
// Attempting to type the refs list
const childRefs = useTemplateRefsList</* What type goes here? */>();
</script>
Problem
I want to type childRefs
such that it correctly represents an array of the recursive component's instances.
Since this
is unavailable in the script setup syntax, I cannot directly refer to the component's instance type.
What I've tried
- Using
ComponentPublicInstance
:
const refs = useTemplateRefsList<ComponentPublicInstance>();
But this type is too generic and doesn’t reflect the actual instance of the recursive component.
- Using
InstanceType
:
const refs = useTemplateRefsList<InstanceType<typeof RecursiveComponent>>();
However, RecursiveComponent isn’t defined yet during its own declaration, which creates a circular dependency issue.
- Typing via
expose
I considered using defineExpose to expose properties/methods and type against those, but this feels like overkill for a simple recursive component.
- Export type from another
ts file
// definitionsFile.ts
import Toto from './Toto.vue';
// Without using an object, I get the following error: Type alias 'TotoType' circularly references itself.
export type TotoType = { a: InstanceType<typeof PermissionSummaryRow> };
// toto.vue
import Toto from './Toto.vue';
// 'childRefs' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer
const childRefs = useTemplateRefsList<TotoType['a']>();
// any
const test = childRefs.value[0]!;
Question
What is the correct way to type useTemplateRefsList
for a recursive component in Vue 3? Is there a way to dynamically reference the current component's type, or is there a workaround to achieve this?
Any help or guidance would be appreciated!
Share Improve this question asked Nov 18, 2024 at 18:17 VinsVins 1941 silver badge9 bronze badges1 Answer
Reset to default 2With Vue 3.5+ you can use useTemplateRef
which is typed automatically, inferred.
There's though a HACK. To make useTemplateRef
to infer the recursive child component, import it with a different name, otherwise the internal instance would be inferred, not the exposed one:
Playground
<script lang="ts" setup>
import CompChild from './Comp.vue';
type Item = {name: string, children?: Item[]};
defineProps<{
data: Item
}>();
import {ref} from 'vue';
import { useTemplateRef } from 'vue';
const $comps = useTemplateRef('$comps');
defineExpose({
addColor(){
color.value =
['red', 'green', 'lime', 'blue', 'orange', 'skyblue'][Math.random()* 6 | 0];
$comps.value?.forEach($comp => $comp.addColor());
}
});
const color = ref('inherit');
</script>
<template>
<div style="padding-left: 24px">
<div>{{ data.name }}</div>
<CompChild ref="$comps" v-if="data.children" v-for="item in data.children" :data="item"/>
</div>
</template>
<style scoped>
div{
background-color: v-bind(color);
}
</style>
I'm working on a recursive Vue 3 component using the Composition API and the useTemplateRefsList
function from @vueuse/core
.
My goal is to collect references to each instance of the recursive component. However, I'm struggling to type the list of references correctly, as the this
keyword is not available in the Composition API.
Here’s a simplified version of my setup:
// toto.vue
<template>
{{ data.name }}
<toto
v-for="item in data.children"
ref="childRefs"
:key="item.id"
:data="item.children"
/>
</template>
<script setup lang="ts">
import { useTemplateRefsList } from '@vueuse/core';
const { data } = defineProps<{
id: string;
name: string;
children?: [
{
id: string;
name: string,
children?: [...]
}
]
}>();
// Attempting to type the refs list
const childRefs = useTemplateRefsList</* What type goes here? */>();
</script>
Problem
I want to type childRefs
such that it correctly represents an array of the recursive component's instances.
Since this
is unavailable in the script setup syntax, I cannot directly refer to the component's instance type.
What I've tried
- Using
ComponentPublicInstance
:
const refs = useTemplateRefsList<ComponentPublicInstance>();
But this type is too generic and doesn’t reflect the actual instance of the recursive component.
- Using
InstanceType
:
const refs = useTemplateRefsList<InstanceType<typeof RecursiveComponent>>();
However, RecursiveComponent isn’t defined yet during its own declaration, which creates a circular dependency issue.
- Typing via
expose
I considered using defineExpose to expose properties/methods and type against those, but this feels like overkill for a simple recursive component.
- Export type from another
ts file
// definitionsFile.ts
import Toto from './Toto.vue';
// Without using an object, I get the following error: Type alias 'TotoType' circularly references itself.
export type TotoType = { a: InstanceType<typeof PermissionSummaryRow> };
// toto.vue
import Toto from './Toto.vue';
// 'childRefs' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer
const childRefs = useTemplateRefsList<TotoType['a']>();
// any
const test = childRefs.value[0]!;
Question
What is the correct way to type useTemplateRefsList
for a recursive component in Vue 3? Is there a way to dynamically reference the current component's type, or is there a workaround to achieve this?
Any help or guidance would be appreciated!
I'm working on a recursive Vue 3 component using the Composition API and the useTemplateRefsList
function from @vueuse/core
.
My goal is to collect references to each instance of the recursive component. However, I'm struggling to type the list of references correctly, as the this
keyword is not available in the Composition API.
Here’s a simplified version of my setup:
// toto.vue
<template>
{{ data.name }}
<toto
v-for="item in data.children"
ref="childRefs"
:key="item.id"
:data="item.children"
/>
</template>
<script setup lang="ts">
import { useTemplateRefsList } from '@vueuse/core';
const { data } = defineProps<{
id: string;
name: string;
children?: [
{
id: string;
name: string,
children?: [...]
}
]
}>();
// Attempting to type the refs list
const childRefs = useTemplateRefsList</* What type goes here? */>();
</script>
Problem
I want to type childRefs
such that it correctly represents an array of the recursive component's instances.
Since this
is unavailable in the script setup syntax, I cannot directly refer to the component's instance type.
What I've tried
- Using
ComponentPublicInstance
:
const refs = useTemplateRefsList<ComponentPublicInstance>();
But this type is too generic and doesn’t reflect the actual instance of the recursive component.
- Using
InstanceType
:
const refs = useTemplateRefsList<InstanceType<typeof RecursiveComponent>>();
However, RecursiveComponent isn’t defined yet during its own declaration, which creates a circular dependency issue.
- Typing via
expose
I considered using defineExpose to expose properties/methods and type against those, but this feels like overkill for a simple recursive component.
- Export type from another
ts file
// definitionsFile.ts
import Toto from './Toto.vue';
// Without using an object, I get the following error: Type alias 'TotoType' circularly references itself.
export type TotoType = { a: InstanceType<typeof PermissionSummaryRow> };
// toto.vue
import Toto from './Toto.vue';
// 'childRefs' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer
const childRefs = useTemplateRefsList<TotoType['a']>();
// any
const test = childRefs.value[0]!;
Question
What is the correct way to type useTemplateRefsList
for a recursive component in Vue 3? Is there a way to dynamically reference the current component's type, or is there a workaround to achieve this?
Any help or guidance would be appreciated!
Share Improve this question asked Nov 18, 2024 at 18:17 VinsVins 1941 silver badge9 bronze badges1 Answer
Reset to default 2With Vue 3.5+ you can use useTemplateRef
which is typed automatically, inferred.
There's though a HACK. To make useTemplateRef
to infer the recursive child component, import it with a different name, otherwise the internal instance would be inferred, not the exposed one:
Playground
<script lang="ts" setup>
import CompChild from './Comp.vue';
type Item = {name: string, children?: Item[]};
defineProps<{
data: Item
}>();
import {ref} from 'vue';
import { useTemplateRef } from 'vue';
const $comps = useTemplateRef('$comps');
defineExpose({
addColor(){
color.value =
['red', 'green', 'lime', 'blue', 'orange', 'skyblue'][Math.random()* 6 | 0];
$comps.value?.forEach($comp => $comp.addColor());
}
});
const color = ref('inherit');
</script>
<template>
<div style="padding-left: 24px">
<div>{{ data.name }}</div>
<CompChild ref="$comps" v-if="data.children" v-for="item in data.children" :data="item"/>
</div>
</template>
<style scoped>
div{
background-color: v-bind(color);
}
</style>
本文标签: javascriptHow to correctly type useTemplateRefsList in a recursive component in Vue 3Stack Overflow
版权声明:本文标题:javascript - How to correctly type useTemplateRefsList in a recursive component in Vue 3? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745602148a2158518.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论