Vue.js: Why won't my data display in this list containing multiple component types?

James N

I am trying to get to a point where I can have multiple component types being rendered within a single parent. It's not something which is easy to search for answers on.

I know that it is differentiating the components by adding something to the templates to distinguish them, but if I pass data as you'll see below, I get nothing, any tips?

Edit: I also don't want to have to render out these components in the HTML for this to work, needs to be passed in via JS.

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<div id="app">
    <component v-for="widget in widgets" :is="widget.type"></component>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.1.10/vue.js"></script>
<script>
    Vue.config.debug = true

    Vue.component('component-a', {
        template: '<p>{{foo}}</p>',
        props: ['foo',]
    });

    Vue.component('component-b', {
        template: '<p>{{bar}}</p>',
        props: ['bar',]
    });

    new Vue({
        el: "#app",
        data: {
            widgets: [
                {
                    type: "component-a",
                    foo: 'Hello'
                },
                {
                    type: "component-b",
                    bar: "World",
                },
            ]
        }
    });
</script>

</body>
</html>
Justin MacArthur

The issue is that you're not passing any props for the foo and bar elements

<!doctype html>
<html>
<head>
    <title></title>
</head>
<body>
<div id="app">
    <component v-for="widget in widgets" :is="widget.type" :foo="widget.foo" :bar="widget.bar"></component>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.1.10/vue.js"></script>
<script>
    Vue.config.debug = true

    Vue.component('component-a', {
        template: '<p>{{foo}}</p>',
        props: ['foo',]
    });

    Vue.component('component-b', {
        template: '<p>{{bar}}</p>',
        props: ['bar',]
    });

    new Vue({
        el: "#app",
        data: {
            widgets: [
                {
                    type: "component-a",
                    foo: 'Hello'
                },
                {
                    type: "component-b",
                    bar: "World",
                },
            ]
        }
    });
</script>

</body>
</html>

jsFiddle

In your example the components are rendering with empty strings so you're not seeing the values you're trying to pass. when using interchangeable components it's often best to use a common value interface. Something along the lines of:

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<div id="app">
    <component v-for="widget in widgets" :is="widget.type" :value="widget.value"></component>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.1.10/vue.js"></script>
<script>
    Vue.config.debug = true

    Vue.component('component-a', {
        template: '<p>{{value.foo}}</p>',
        props: ['value',]
    });

    Vue.component('component-b', {
        template: '<p>{{value.bar}}</p>',
        props: ['value',]
    });

    new Vue({
        el: "#app",
        data: {
            widgets: [
                {
                    type: "component-a",
                    value: {
                        foo: 'Hello'
                    }
                },
                {
                    type: "component-b",
                    value: {
                        bar: "World",
                    }
                },
            ]
        }
    });
</script>

</body>
</html>

So that the components only pass the single value object to the component necessary and the component pulls the unique values from that object.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why won't my Bootstrap 3 button icons display?

From Dev

Why won't my controls display when added to a tab programatically

From Dev

Why won't my list populate? IndexError: List Out of Range

From Dev

Why won't my form display?

From Dev

Custom component won't display nested components

From Dev

My Horizontal Navigation Bar Won't Display Horizontally, Why?

From Dev

Why won't cron execute my node.js script?

From Dev

Why won't my simple Angular.js app work?

From Dev

Why won't my List work?

From Dev

Why Won't My HTML Recognize My Angular JS

From Dev

Why can't List contain multiple types?

From Dev

Why won't my express js app execute?

From Dev

Why won't the text display

From Dev

Why won't my random images display in browser?

From Dev

Cookie data won't display

From Dev

Why won't IE display my WPF component?

From Dev

Why won't my list align horizontally center

From Dev

Why won't my taskbar display on my secondary monitor?

From Dev

why won't the background image display in my code?

From Dev

UITableView Won't Display Data

From Dev

Obj-C: Why won't my image display in NSImageView?

From Dev

PDO won't update my data in the database, why?

From Dev

Vue.js won't adhere to multiple instances

From Dev

Why won't ls display output in multiple columns, for some directories?

From Dev

Why won't my data go into my database?

From Dev

Why won't the cells in my table view display any text?

From Dev

Why won't my Collection View Cells display in the iPhone Simulator?

From Dev

Why won't my .html file connect to .js file?

From Dev

Why won't my DIV change to display='none' (onclick JavaScript)?

Related Related

HotTag

Archive