Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vuejs nested components with inline-template

I'm going to end up having a LOT of different components nested for each page. I have a component view for each page in my application. On each page, there are different Vue instances that will re-use components I have made such as slider's, tabber's, carousel, etc.

I'm trying to restructure this since a lot of Vue instances were interfering with each other and I realized I should only have one main Vue instance with a lot of inner components.

This is what I have set up so far:

http://jsfiddle.net/jmtg5r4s/

The problem is is that it stops after loading the home view component. It won't load any nested components unless I have a template set for them which I don't want to do because I want to take advantage of Laravel blade syntax and not use regular HTML. Plus all of my server-side helpers, etc.

Javascript:

var App = new Vue({
    el: '#app',

    attached: function() {
        console.log('main app loaded');
    },

    components: {
        'home-view': {

            attached: function() {
                console.log('home view loaded');
            },

            components: {
                'home-slider': {

                    attached: function() {
                        console.log('homepage slider loaded');
                    },

                    components: {
                        'slider': {

                            template: '#slider-template',
                            replace: true,

                            attached: function() {
                                console.log('general slider component loaded');
                            },

                            components: {
                                'slide': {

                                    template: '#slide-template',
                                    replace: true,

                                    props: ['index', 'text'],

                                    attached: function() {
                                        console.log('general slide component loaded');
                                    }

                                }
                            }

                        }
                    }

                }
            }

        }
    }
});

HTML:

<script type="x-template" id="slider-template">
    <div class="slider">
        <content></content>
    </div>
</script>

<script type="x-template" id="slide-template">
    <div class="slide" id="slide{{ index }}">
        {{ text }}
    </div>
</script>

<div id="app">
    <component is="home-view">
        <div id="slideshow" v-component="slider" inline-template>
            <slider>
                <slide index="1" text="Slide #1"></slide>
                <slide index="2" text="Slide #2"></slide>
            </slider>
        </div>

        <p>More content specific to the homepage here.</p>
    </component>
</div>

I might be overthinking this, but thank you for any help/ideas!

like image 952
HaleyBuggs Avatar asked Sep 17 '15 15:09

HaleyBuggs


2 Answers

I had a similar problem. You can solve it by putting the "inline-template" attribute on your 'home-view' component aswell:

<div id="app">
<component inline-template is="home-view">
    <div id="slideshow" v-component="slider" inline-template>
        <slider>
            <slide index="1" text="Slide #1"></slide>
            <slide index="2" text="Slide #2"></slide>
        </slider>
    </div>

    <p>More content specific to the homepage here.</p>
</component>

The innerHTML of the component is considered the components template if the inline-template attribute it applied.

Reference on Vue.js docs

like image 51
Lee Overy Avatar answered Oct 21 '22 17:10

Lee Overy


You are mixing up the concept of a template for a component with existing content that might exist in a component when compiled. In AngularJS, this is referred to as "transclusion". In Vue, it is referred to as "Content Insertion" and you can read more about it here:

http://vuejs.org/guide/components.html#Content_Insertion

Basically, you need to use the Content Insertion techniques to get the effect of reusing content present in a component when it is compiled. Otherwise, a component assumes all of its content comes from its template.

like image 45
David K. Hess Avatar answered Oct 21 '22 17:10

David K. Hess