Vue学习

作用域插槽

2018-08-03  本文已影响0人  椰果粒
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>插槽</title>
    <script src="https://unpkg.com/vue@2.5.16/dist/vue.js"></script>
</head>
<body>
    <div id="app">
        <child>
            
        </child>
    </div>
    <script>
        var child = {
            data : function(){
                return {
                    lists : [1,2,3,4,5,6]
                }
            },
            template : `<div>
                <ul>
                    <li v-for="item of lists">{{item}}</li>
                </ul>
            </div>`
        }
        var vm = new Vue({
            el : "#app",
            components : {
                child : child
            }
        })
    </script>
</body>
</html>
<body>
    <div id="app">
        <child>
            <!-- template是固定写法,props是传递过来的值 -->
            <template slot-scope="props">
                <h1>{{props.item}}</h1>
            </template>
        </child>
    </div>
    <script>
        var child = {
            data : function(){
                return {
                    lists : [1,2,3,4,5,6]
                }
            },
            template : `<div>
                <ul>
                    <slot 
                        v-for="item of lists"
                        :item=item
                    ></slot>
                </ul>
            </div>`
        }
        var vm = new Vue({
            el : "#app",
            components : {
                child : child
            }
        })
    </script>
</body>
上一篇 下一篇

猜你喜欢

热点阅读