vue slot作用域插槽 slotProps 传值

子组件

<template>
	<td v-for="column in columns" :key="`${column.key}_${index}`">
	   <slot
	     v-if="column.slot"
	     :name="column.key"
	     :record="item"
	     :index="index"
	   ></slot>
	 </td>
</template>
<script>
	export default {
		data() {
			columns: [
 						  { key: 'ranking', title: '排名', slot: 'ranking' },
						  { key: 'range', title: '城市' },
						  { key: 'satisfaction', title: '满意度', },
						  { key: 'bsc', title: '触点', slot: 'bsc' }
					]
			}
	}
</script>

父组件

<template>
	<list class="list-wrapper">
		<template #ranking="slotProps">
			{{slotProps}} // { "index": 0, "record": { "key": "ranking", "title": "排名", "slot": "ranking", "index": 0 } }
		</template>
		<template #bsc="slotProps">
            <li>{{slotProps}}</li> // { "index": 3, "record": { "key": "bsc", "title": "触点", "slot": "bsc", "index": 3 } }
            <li>{{slotProps.record}}</li> // "record": { "key": "bsc", "title": "触点", "slot": "bsc"}
        </template>
	</list>
</template>

总结:slotProps是当前插槽bind绑定的所有数据
小计:vue component 动态组件可以用is判断当前渲染的组件,以及mixins可以用作项目中的整体逻辑设置

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Vue 3中的作用域插槽是一种功能强大的特性,它允许你在父组件中向子组件传递内容,并且还能让子组件对这些内容进行处理和渲染。 使用作用域插槽,你可以在子组件中定义插槽的具体渲染规则,并且可以在父组件中通过具名插槽的方式向子组件传递内容。子组件可以通过`v-slot`指令来接收父组件传递的内容,并在插槽内部进行渲染。 在Vue 3中,作用域插槽的语法有所改变。你可以使用`<slot>`元素加上`name`属性来定义具名插槽,在父组件中使用`v-slot`指令来向该具名插槽传递内容。例如: ```html <!-- 子组件 --> <template> <div> <slot name="header"></slot> <slot></slot> <slot name="footer"></slot> </div> </template> <!-- 父组件 --> <template> <div> <child-component> <template v-slot:header> <!-- 这里的内容将会传递给子组件的具名插槽header --> </template> <!-- 这里的内容将会传递给子组件的默认插槽 --> <template v-slot:footer> <!-- 这里的内容将会传递给子组件的具名插槽footer --> </template> </child-component> </div> </template> ``` 在Vue 3中,还支持使用缩写语法来定义默认插槽和具名插槽。例如,`<template v-slot:header>`可以简写为`<template #header>`,`<template v-slot:footer>`可以简写为`<template #footer>`。 通过使用作用域插槽,你可以更灵活地向子组件传递内容,并让子组件有更大的自由度来处理和渲染这些内容。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值