Can not use v-for directive on prop passed to Vue component

1

Follow the code:

<!DOCTYPE html>
<html lang="pt-br">
    <head>
        <meta charset="utf-8" />
        <title>Passagem de valores com VueJS</title>
    </head>
    <body>

        <div id="app">
            <my-ul>
                <my-li></my-li>
            </my-ul>
        </div>

        <template id="my-li">
            <li v-for="item in items">
                {{ item }}
            </li>
        </template>

        <template id="my-ul">
            <ul>
                <my-li :items="['Bala', 'Chocolate']"></my-li>
            </ul>
        </template>

        <script src="https://unpkg.com/vue"></script><script>Vue.component("my-li", {
                template: "#my-li",
                props: ["items"],
            });

            Vue.component("my-ul", {
                template: "#my-ul",
                data: function() {
                    return {

                    }
                },
            });

            var app = new Vue({
                el: "#app",
                data: {
                    message: "Hello Vue"
                }
            });
        </script>
    </body>
</html>

If I just try to check if prop items is being passed this way:

<template id="my-li">
    <li>
        {{ items }}
    </li>
</template>

I have as output:

[ "Bala", "Chocolate" ] 

But when I try to apply the v-for directive I simply get a blank screen, but I can not seem to identify where I'm going wrong. Any help is welcome.

    
asked by anonymous 14.10.2018 / 02:46

1 answer

3

You're having trouble rendering because you're using DOM Template, at documentation on the particularities of DOM Templates.

Basically, you need to use the is attribute. As <ul> only accepts <li> as child tag, you write the <li> tag and say in the is attribute which component you want to render there.

See the examples, meu-ul is the correction of the code you submitted and meu-ul-2 is what I believe you would like to use, with the

14.10.2018 / 04:33