Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested vue.js instances/elements

This may sound like a real noob question, but I'm pretty new to MVVM... or even MVC in JS, so sorry in advance.

I'm playing about with vue.js, and loving the simplicity of it so far. But for what I am trying to do, I think I need to go about it a different way.

I want to nest Vue instances/elements inside each other, but of course, the parent will then use the child when it reads through the DOM on init.

For the sake of arguments, below is an example of what I mean, I am not doing anything like this, but this is the simplest way to example what I mean:

<body>
    {{ message }}
    <div id="another">
        {{ message }}
    </div>
</body>

Then my JS for example would be:

new Vue({
    el: "body",
    data: {
        message: "I'm the parent"
    }
});

new Vue({
    el: "#another",
    data: {
        message: "I'm the child"
    }
});

The outcome would be:

<body>
    I'm the parent
    <div id="another">
        I'm the parent
    </div>
</body>

Now I completely understand why it is doing this and in fact, it should do this, but my example is just trying to illustrate how I would do something like this?

In my real life project, I have a v-class on my body that changes when stuff happens in the body (in numerous places) but of course my body will also want other instances of vue that do other stuff.

how would I go about nesting? Is there feature in vue to deal with this? Do I need to deal with components? Or maybe, fetch the body from within a child element (e.g. like jQuery would with $("body")) then manipulate it within the Vue instance?

Hope this question isn't too stupid and someone can point me in the right direction.

Thanks

like image 726
keogh Avatar asked Jul 10 '15 15:07

keogh


1 Answers

Think components. http://vuejs.org/guide/components.html

Create a Vue instance on the body as you have above, but anything nested in that is a component. Pass data via props.

like image 144
Fergal Avatar answered Oct 13 '22 00:10

Fergal