插槽 v-slot

  • 具名插槽
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <!-- 父组件 -->
        <div id="app">
            <cpn>
                <template v-slot:header>开始部分</template>
                <template v-slot:default>中间部分</template>
                <template v-slot:footer>结尾部分</template>
            </cpn>
        </div>
        
        <!-- 子组件 -->
        <template id="npv">
            <div>
                <header>
                    <!-- 我们希望把页头放这里 -->
                     <slot name="header"></slot>
                </header>
                <main>
                    <!-- 我们希望把主要内容放这里 -->
                     <slot></slot>
                </main>
                <footer>
                    <!-- 我们希望把页脚放这里 -->
                    <slot name="footer"></slot>
                </footer>
            </div>
        </template>
        
        <script src="js/vue.js"></script>
        <script type="text/javascript">
            
            const vm = new Vue({
                el: '#app',
                components: {
                    cpn: {
                        template: '#npv',
                    }
                }
            });
            
        </script>
    </body>
</html>
  • 作用域插槽 把子组件中的数据拿到父组件中使用
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <!-- 父组件 -->
        <div id="app">
            <cpn>
                <!-- :后面的为slot的name ,=后面的为数据 -->
                <template v-slot:childlist="list">
                    <span v-for="item in list.list">{{item}} * </span>
                </template>
            </cpn>
        </div>
        
        <!-- 子组件 -->
        <template id="npv">
            <div>
                <slot>
                    <ul>
                        <li v-for="item in childList">{{item}}</li>
                    </ul>
                </slot>
                <!-- 修改中间的显示方式 -->
                <slot name="childlist" v-bind:list="childList">
                    <ul>
                        <li v-for="item in childList">{{item}}</li>
                    </ul>
                </slot>
                
                <slot>
                    <ul>
                        <li v-for="item in childList">{{item}}</li>
                    </ul>
                </slot>
            </div>
        </template>
        
        <script src="js/vue.js"></script>
        <script type="text/javascript">
            
            const vm = new Vue({
                el: '#app',
                components: {
                    cpn: {
                        template: '#npv',
                        data() {
                            return {
                                childList: ["PHP", "Java", "Go", "c++", "JavaScript"],
                            }
                        }
                    }
                }
            });
            
        </script>
    </body>
</html>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。