Home  >  Q&A  >  body text

Recursion in single file components in Vue.js 3

How to use recursive components in Vue3?

Using recursive components like normal components in Vue 3 will result in errors Cannot be accessed before initialization

Tree.vue:

<template>
  <Tree v-if="hasChildren" />
</template>

<script lang="ts">
import Tree from './Tree.vue';

export default defineComponent({
  components: {
    Tree
  },

  setup() {

    const hasChildren = someExitRecursionCondition();

    return {
      hasChildren
    }
  }
</script>


P粉710478990P粉710478990289 days ago564

reply all(2)I'll reply

  • P粉176151589

    P粉1761515892023-11-24 00:44:05

    You can provide only the component name option:

    
    
    sssccc
    
    

    reply
    0
  • P粉330232096

    P粉3302320962023-11-24 00:35:52

    document:

    Components can be imported by their filename, but do not need to be listed in the components settings object. However, it is enough to use the named component in the template without importing it.

    Tree.vue:

    
    
    sssccc
    

    reply
    0
  • Cancelreply