Vue中v-slot插槽的使用

本文介绍了Vue中v-slot的使用,包括匿名插槽、具名插槽和作用域插槽的应用。从vue2.6.0版本开始,v-slot替代了slot-scope和slot,但template中仍使用slot声明插槽。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Vue中v-slot插槽的使用

在vue2.6.0以上版本v-slot将取代slot-scope和slot。
这里取代的意思是:v-slot在调用时取代了 slot 和 slot-scope,而在template中声明时仍会用solt进行声明插槽。

  1. 匿名插槽
<!-- 1.匿名插槽 -->
    <div id="app1">
        <cpn1>
            <template v-slot>
            
          </template>
        </cpn1>
    </div>

    <script>
        Vue.component('cpn1', {
            template: `
          <h1><slot>我是匿名插槽</slot></h1>
        `
        })
        const app = new Vue({
            el: "#app1"
        })

2.具名插槽

<!-- 2.具名插槽 -->
    <div id="app2">
        <cpn2>
            <template v-slot:header>
            
          </template>
        </cpn2>
    </div>
    <script>
        Vue.component('cpn2', {
            template: `
          <h1><slot name="header">我是具名插槽</slot></h1>
        `
        })
        const app2 = new Vue({
            el: "#app2"
        })
    </script>

3.作用域插槽

<!-- 3.作用域插槽 -->
    <div id="app3">
        <cpn3>
            <!-- default是指在编译时会去子级作用域中取当前子组件中的user属性 -->
            <template v-slot:default="slotProps">
          {{slotProps.user.name}}
        </template>
        </cpn3>
    </div>
    <script>
        Vue.component('cpn3', {
            template: `
          <h1><slot v-bind:user="user">{{user.firstName}}</slot></h1>
        `,
            data() {
                return {
                    user: {
                        name: "阿玮",
                        firstName: "郑"
                    }
                }
            }
        })
        const app3 = new Vue({
            el: "#app3",
        })
    </script>

全部代码:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/vue.js"></script>
</head>

<body>
    <!-- 1.匿名插槽 -->
    <div id="app1">
        <cpn1>
            <template v-slot>
            
          </template>
        </cpn1>
    </div>

    <script>
        Vue.component('cpn1', {
            template: `
          <h1><slot>我是匿名插槽</slot></h1>
        `
        })
        const app = new Vue({
            el: "#app1"
        })
    </script>
    <!-- 2.具名插槽 -->
    <div id="app2">
        <cpn2>
            <template v-slot:header>
            
          </template>
        </cpn2>
    </div>
    <script>
        Vue.component('cpn2', {
            template: `
          <h1><slot name="header">我是具名插槽</slot></h1>
        `
        })
        const app2 = new Vue({
            el: "#app2"
        })
    </script>
    <!-- 3.作用域插槽 -->
    <div id="app3">
        <cpn3>
            <!-- default是指在编译时会去子级作用域中取当前子组件中的user属性 -->
            <template v-slot:default="slotProps">
          {{slotProps.user.name}}
        </template>
        </cpn3>
    </div>
    <script>
        Vue.component('cpn3', {
            template: `
          <h1><slot v-bind:user="user">{{user.firstName}}</slot></h1>
        `,
            data() {
                return {
                    user: {
                        name: "阿玮",
                        firstName: "郑"
                    }
                }
            }
        })
        const app3 = new Vue({
            el: "#app3",
        })
    </script>
</body>

</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值