vue作用域插槽

房东的猫 提交于 2019-12-05 03:14:25

   简而言之,作用域插槽就是让插槽内容能够访问子组件中的数据。


案例如下:有CurUser组件

<template>
    <span>
        <!-- 在slot 元素上绑定user,让父组件插槽能够访问 -->
        <slot :user="user"></slot>
    </span>
</template>

<script>
    export default {
        data() {
            return {
                user: {
                    firstName: "张",
                    lastName: "三"
                }
            }
        }
    }
</script>
<style scoped>
</style>

使用组件

        <cur-user>
            <!-- 使用slot-scope 或者v-slot 来接收-->
            <template slot-scope="scope">
                {{scope.user.firstName}} {{scope.user.lastName}}
            </template>
        </cur-user>

 

 

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!