Vue——slot插槽

<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <title>Vue中的插槽(slot)</title>
        <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    </head>

    <body>
        <div id="root">
            <child>
                <p>Harold</p>
            </child>

            <body-content>
                <div class="header" slot='header'>header</div>
                <div class="footer" slot='footer'>footer</div>
            </body-content>
        </div>
        <script type="text/javascript">
            Vue.component('child', {
                template: `<div>
                                        <p>hello</p>
                                        <slot>默认内容</solt>
                                    </div>`
            })

            Vue.component('body-content', {
                template: `<div>
                                        <slot name='header'></slot>
                                        <div class='content'>content</div>
                                        <slot name='footer'></slot>
                                    </div>`
            })

            var vm = new Vue({
                el: '#root'
            })
        </script>
    </body>

</html>

猜你喜欢

转载自www.cnblogs.com/Harold-Hua/p/11754014.html