Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vuejs recursive single file components

Tags:

vue.js

vuejs2

Unknown custom element: - did you register the component correctly? For recursive components, make sure to provide the "name" option.

I have a Sidebar that contains a TreeList that contains TreeNodes that each contain a TreeList.

I have read plenty about other people having problems with this. I have tried solutions such as specifying kebab case names for the components. I have read about this being a problem with namespace and I agree, because I have an example working with all components in a single file. When the components are in separate files the above error occurs when reloading my script using vue CLI. Occasionally and without explanation when saving my file the error goes away and the recursion works again.

Then when it works correctly there is an error when clicking on child nodes.

Cannot read property 'length' of undefined at simpleNormalizeChildren

Is it something do with vue CLI serve? I am open to declaring these things in the global namespace except I'm not sure how that would work exactly.

import TreeList from './TreeList.vue'

export default {
    name: 'tree-node',
    components: {
        'tree-list': TreeList
    },

...

import TreeNode from './TreeNode.vue'

export default {
    name: 'tree-list',
    components: {
        'tree-node': TreeNode
    }

...

import TreeList from './TreeList.vue'
import TreeNode from './TreeNode.vue'

export default {
    name: 'Sidebar',
    components: {
        TreeList,
        TreeNode
    }
like image 362
Ddefin Orsstt Avatar asked Dec 12 '18 16:12

Ddefin Orsstt


People also ask

What are recursive components Vue?

A Vue component is considered recursive if it references itself within its own template. Recall that we mentioned that components are designed to be reused in other components.

What are Vue single-file components?

A single-file component is composed of three parts: The <template> section which contains the component's markup in plain HTML. The <script> section which contains all the JS logic within that component. The <style> section which contains all the component styles.

What is $parent in Vuejs?

Accessing the Parent Component Instance Similar to $root , the $parent property can be used to access the parent instance from a child.

Can a component call itself Vue?

Recursion and Vue Components In Vue, recursion is very much possible and quite useful. I mean, not only in Vue, we can follow the rules above to implement recursive behavior in any framework. So, based on the given definition, we can say that a recursive component is a component invoking itself.


1 Answers

This is probably happening because of the circular referencing between your TreeList and TreeNode components ..

When you look closely, you’ll see that these components will actually be each other’s descendent and ancestor in the render tree - a paradox!

To resolve this paradox you can either register your components globally using Vue.component, or you can defer the import of one of your components to a later point (by moving your import to the beforeCreate hook or using async components as demonstrated here) ..

TreeList.vue

export default {
    name: 'tree-list',
    components: {
        'tree-node': () => import('./TreeNode.vue')
    }
    ...
}
like image 85
Husam Ibrahim Avatar answered Sep 28 '22 14:09

Husam Ibrahim