vue 动态插槽 v-slot #

本文介绍了如何在Vue中使用动态插槽减少template代码,重点讲解了v-slot的动态绑定和#语法的使用,结合实例展示了如何在el-table组件中应用动态插槽,以及Vue2.6的新特性。动态插槽是提升代码复用性和组织复杂UI的有效手段。
摘要由CSDN通过智能技术生成

vue 动态插槽

今天遇到一个需求要动态插槽减少template中的代码量,查了好多资料,发现讲的都不够细致,
把我最后实现效果展现出来:

    <el-table
      :columns="columns"
      :data="list"
		<template
		   v-for="item in templateSlot"
		   v-slot:[item]="scope"
		 >
		   <div :key="item">
		     {{ scope.scope.row[item] }}
		   </div>
		 </template>
	</el-table>
	
	  data() {
	    return {
	      // template slot列表
	      templateSlot: [
	        'peom1',
	        'author',
	      ],
	    }
	  }

可以看到主要点在

v-slot:[item]="scope"

如果是写死的情况

v-slot:pemo1="scope"

使用括号 可以动态绑定,且scope可以绑定当前行的数据,用于下面使用。

这部分是vue2.6新增的语法,之前写法是

slot="pemo1"
slot-scope="scope"

其实vue2.6还提供了另一种方式,即通过# 来实现slot的绑定,写法如下

<template #pemo1 >
	 {{  }}
</template>
<template #pemo1="{ scope }" >
	{{ scope.pemo1 }}
</template>

举一反三吧,最上面的写法就如下了:

    <el-table
      :columns="columns"
      :data="list"
		<template
		   v-for="item in templateSlot"
		   #[item]="scope"
		 >
		   <div :key="item">
		     {{ scope.scope.row[item] }}
		   </div>
		 </template>
	</el-table>
	
	  data() {
	    return {
	      // template slot列表
	      templateSlot: [
	        'peom1',
	        'author',
	      ],
	    }
	  }

其他插槽用法还有很多,插槽真的博大精深 还需研究。

  • 6
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值