Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Vue 3 debugging reference for reactivity issues, computed errors, watcher bugs, async failures, and SSR hydration problems.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
reference/self-referencing-component-name.md
1---2title: Self-Referencing Components Use Filename as Implicit Name3impact: LOW4impactDescription: Understanding this avoids confusion with recursive components5type: gotcha6tags: [vue3, component-registration, self-reference, recursive-components, sfc]7---89# Self-Referencing Components Use Filename as Implicit Name1011**Impact: LOW** - In Single-File Components (SFC), a component can reference itself in its own template using its filename as the component name. This is useful for recursive components like tree structures or nested comments. However, this implicit registration has lower priority than explicitly imported components, which can cause confusion.1213## Task Checklist1415- [ ] Use the filename (without extension) to self-reference a component16- [ ] Be aware that imported components take precedence over self-reference17- [ ] For clarity in recursive components, consider explicit naming1819**Example:**20```vue21<!-- TreeItem.vue -->22<script setup>23defineProps({24item: Object25})26</script>2728<template>29<div class="tree-item">30<span>{{ item.name }}</span>31<!-- Self-reference using filename -->32<TreeItem33v-for="child in item.children"34:key="child.id"35:item="child"36/>37</div>38</template>39```4041```vue42<!-- Comment.vue - recursive comments -->43<script setup>44defineProps({45comment: Object46})47</script>4849<template>50<div class="comment">51<p>{{ comment.text }}</p>52<div class="replies" v-if="comment.replies?.length">53<!-- Self-reference for nested replies -->54<Comment55v-for="reply in comment.replies"56:key="reply.id"57:comment="reply"58/>59</div>60</div>61</template>62```6364## Priority: Imports Override Self-Reference6566```vue67<!-- FooBar.vue -->68<script setup>69// If you import a component named FooBar, it takes precedence70import FooBar from './different/FooBar.vue'71</script>7273<template>74<!-- This renders the IMPORTED FooBar, not this file -->75<FooBar />76</template>77```7879To explicitly self-reference when there's a naming conflict:8081```vue82<!-- FooBar.vue -->83<script setup>84import OtherFooBar from './different/FooBar.vue'85// No way to explicitly import "self" in script setup86// Must rename the import to avoid conflict87</script>8889<template>90<OtherFooBar />91<!-- FooBar still refers to self (this file) because92the import was aliased -->93<FooBar />94</template>95```9697## Options API: Explicit Name Option9899```vue100<!-- RecursiveList.vue -->101<script>102export default {103name: 'RecursiveList', // Explicit name for self-reference104props: {105items: Array106}107}108</script>109110<template>111<ul>112<li v-for="item in items" :key="item.id">113{{ item.name }}114<RecursiveList v-if="item.children" :items="item.children" />115</li>116</ul>117</template>118```119120## Common Use Cases for Self-Reference1211221. **Tree structures** - File explorers, org charts1232. **Nested comments** - Reddit-style comment threads1243. **Menu navigation** - Recursive dropdown menus1254. **Category hierarchies** - Product categories, taxonomies126127## Avoid Infinite Recursion128129```vue130<!-- TreeNode.vue -->131<script setup>132defineProps({133node: Object,134maxDepth: { type: Number, default: 10 },135currentDepth: { type: Number, default: 0 }136})137</script>138139<template>140<div class="node">141{{ node.name }}142<!-- Guard against infinite recursion -->143<template v-if="node.children && currentDepth < maxDepth">144<TreeNode145v-for="child in node.children"146:key="child.id"147:node="child"148:max-depth="maxDepth"149:current-depth="currentDepth + 1"150/>151</template>152</div>153</template>154```155156## Reference157- [Vue.js Component Registration](https://vuejs.org/guide/components/registration.html)158