Vue 作用域插槽slot

書院二層樓發表於2020-10-02
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <title>作用域插槽</title>
</head>
<body>
    <div id="test">

    </div>

    <script>

        //作用域插槽
        Vue.component("ComponentB", {
            template:`
            <div>
                我是子元件B
                <br>
                <!--繫結count和childTile-->
                <slot name="header" :cn="count" :ct="childTile"></slot>      
            </div>
            `,
            data() {
                return {
                    count: 8,
                    childTile:"我是子元件的Title",
                }
            },
        })
        const app = new Vue({
            el:"#test",
            data:{},
            template:`
            <div>
                <ComponentB>
                <!--header插槽可以使用count和childTile-->
                <template v-slot:header="data">傳給ComponentBheader插槽001   {{data.cn}}---- {{data.ct}}</template>
                </ComponentB>
            </div>
            `
        })
    </script>
    
</body>
</html>

效果截圖:

相關文章