Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have indirectly self nested components in Vue JS 2?

I manage to create directly self nested components using the name property and all works perfectly.

<template>
    <div>
        <span>Hi, I'm component A!</span>
        <component-a></component-a>
    </div>
</template>

<script>
    export default {
        name: 'component-a',
        components: {
            'component-a': this
        }
    }
</script>

Now, I want to create indirectly self nested components. Something like this:

ComponentA.vue:

<template>
    <div>
        <span>Hi, I'm component A!</span>
        <component-b v-if="hasItems" v-for="item in items" :item="item"></component-b>
    </div>
</template>

<script>
    import ComponentB from './ComponentB.vue'

    export default {
        name: 'component-a',
        components: {
            'component-b': ComponentB
        },
        props: ['items'],
        computed: {
            hasItems() {
                return this.items.length > 0
            }
        }
    }
</script>

ComponentB.vue:

<template>
    <div>
        <span>Hi, I'm component B!</span>
        <component-a v-if="hasItems" :items="item.items"></component-a>
    </div>
</template>

<script>
    import ComponentA from './ComponentA.vue'

    export default {
        name: 'component-b',
        components: {
            'component-a': ComponentA
        },
        props: ['item'],
        computed: {
            hasItems() {
                return this.item.items.length > 0
            }
        }
    }
</script>

But that fails. I get the following error:

[Vue warn]: Failed to mount component: template or render function not defined. (found in component )

Has anyone came across something like this and was able to solve it? According to the documentation we have control recursive components with conditional rendering that what I am doing... I even tried to use the name prop on the components but it did nothing (nor I think it should since the components are not directly self-nested).

like image 476
António Quadrado Avatar asked Oct 22 '16 16:10

António Quadrado


People also ask

How do you put a component inside another component in Vue?

You need to create your portfolio component first e.g. in src/components/Projects/Portfolio. vue and then import it inside the script tag within your Landing. vue component.

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.

How do I hide components in Vue?

Conclusion. Vue gives you a bunch of good ways to hide the element on the screen. When using v-if="false" the element isn't rendered at all in the DOM. When using v-show="false" the element is rendered in the DOM, however, Vue applies the inline style display: none that hides the element completely.


1 Answers

I tried your code and I also ended up with the same error, without a clue on how to proceed. Later I closed my vue-cli and tried directly using vue.js from a CDN (standalone version), and it worked alright.

Here is the working example: https://jsfiddle.net/mani04/z09Luphg/

There is no magic going on here. Component A and Component B call each other with a counterValue. Once the counterValue reaches some limit, the recursion stops.

If you do not get a better answer, and if you are unable to modify your app architecture, you can try using this standalone vue.js method.

EDIT: more info below

On further research today, I came across this github discussion on webpack cyclical imports / circular dependencies: https://github.com/webpack/webpack/issues/1788

The standalone jsFiddle example above does not require any ES6 imports. In my sample code, Vue.js registers components globally before initiating the app. Therefore it works without any issues.

In summary, this does not look like an issue with Vue.js, but a webpack / es6 limitation based on current info. I may be wrong, please keep exploring further!

like image 120
Mani Avatar answered Sep 27 '22 22:09

Mani