Vue中的插槽

本文详细介绍了Vue.js中插槽的使用,包括一般插槽、具名插槽和作用域插槽,以及它们在父子组件间动态内容传递中的应用和示例。
摘要由CSDN通过智能技术生成

一般的插槽 可以通过以下方式进行实现
父组件

<template>
	<Son>这一句话将放入Son组件的slot标签内</Son>
</template>

子组件

<template>
	<div>
        //slot标签内部的文本 在父组件没有传递内容过来时 进行显示   属于默认内容
        <slot>/*我的内容由父组件使用时传入*/</slot>
    </div>
</template>

具名插槽

对slot组件 使用name属性 在父组件内使用v-slot:name 绑定在子组件标签内的template标签身上 是的template标签内的内容 会传入 子组件中属性为name的slot标签内

//子组件结构

<template>
	<div>
        <slot name='head'></slot>
    </div>
    <div>
        <slot name='body'></slot>
    </div>
    <div>
        <slot name='footer'></slot>
    </div>
</template>

//父组件结构
<template>
	<Son>
    	<template v-slot:head>
			我要去head里面
		</template>
        <template v-slot:body>
			我要在body里面显示
		</template>
        <template v-slot:footer>
			我要在footer里面显示
		</template>
    </Son>
</template>

作用域插槽

<template>
	<div v-for='item in xxx'>
        <slot msg='132' :row='item'></slot>
        //通过slot插槽 将slot标签的属性 以对象的方式传输至 父组件中
        /*	{
        		row:{id:xxx , name:xxx, age:xxx}//row 里面是item的信息
        		msg:'132'
        	}*/
    </div>
</template>

//父组件接收
<template>
	<Son :data='list'>
        
        //在父组件中 通过template #插槽名='变量名' 对slot标签传输的数据进行接收 对变量名无要求   如果没有设置插槽名  可以用默认的#default  进行接收 
    	<template #default = 'obj'>
			我要去head里面
		</template>
    </Son>
</template>
  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值