Vue 15-具名插槽、作用域插槽

一.具名插槽

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>插槽slot</title>
    <script src="./js/vue.js"></script>
</head>
<body>
    <div id="root">
        <body-content>
            <!-- slot="插槽名" 定义插槽名 -->
            <div class="header" slot="header">I am header</div>
            <div class="footer" slot="footer">I am footer</div>
        </body-content>
    </div>
    <script>
        Vue.component('body-content', {
            // 通过<slot name="">使用具名插槽
            template:`<div>
                        <slot name="header"></slot>
                        <div class="content">content</div>
                        <slot name="footer">
                            <em>defalut ftext</em>
                        </slot>
                      </div>`
        })
        var vm = new Vue({
            el: '#root',           
        })
    </script>
</body>
</html>

二.作用域插槽

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="./js/vue.js"></script>
</head>
<body>
    <div id="root">
        <child>
            <template slot-scope="props">
                <li>{{props.item}}</li>
            </template>
        </child>
    </div>
    <script>
        Vue.component('child', {
            data: function(){
                return {
                    list: [1,2,3,4]
                }
            },
            template: `<div>
                            <ul>
                                <slot v-for="item of list"
                                    :item=item                                
                                ></slot>
                            </ul>
                        </div>`
        })

        var vm = new Vue({
            el: '#root',
        })
    </script>
</body>
</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue.js中,有两种类型的插槽具名插槽作用域插槽。 1. 具名插槽(Named Slots): 具名插槽允许您在组件中定义多个插槽,并在使用组件时根据插槽名称分发内容。这样可以更灵活地控制组件的结构和内容。具名插槽可以通过`<slot>`元素的`name`属性来定义,并且在使用组件时使用相应的`<template>`元素来填充内容。 示例: ```html <!-- MyComponent.vue --> <template> <div> <slot name="header"></slot> <slot></slot> <slot name="footer"></slot> </div> </template> <!-- 使用 MyComponent.vue --> <template> <my-component> <template v-slot:header> <!-- 插入头部内容 --> </template> <!-- 默认插槽 --> <!-- 插入主要内容 --> <template v-slot:footer> <!-- 插入底部内容 --> </template> </my-component> </template> ``` 2. 作用域插槽(Scoped Slots): 作用域插槽允许您将父组件中的数据传递给子组件,并在子组件中自定义渲染逻辑。这使得子组件更加灵活和可配置。作用域插槽通过使用`<slot>`元素的`name`属性和`v-slot`指令来定义,并在使用组件时传递数据给插槽。 示例: ```html <!-- MyComponent.vue --> <template> <div> <slot name="item" v-for="item in items" :item="item"></slot> </div> </template> <!-- 使用 MyComponent.vue --> <template> <my-component> <template v-slot:item="slotProps"> <!-- 使用 slotProps.item 渲染每个项目 --> </template> </my-component> </template> ``` 这样,父组件中的`items`数组的每个元素都会传递给子组件中的作用域插槽,子组件可以根据传递的数据进行自定义渲染。 希望以上解释对您有所帮助,如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值