当前位置:网站首页>组件传值-作用域插槽
组件传值-作用域插槽
2022-08-09 22:59:00 【Drizzlejj】
在父组件中使用子组件中的内容
作用域插槽其实就是带数据的插槽,即带参数的插槽,简单的来说就是子组件提供给父组件的参数,该参数仅限于插槽中使用,父组件可根据子组件传过来的插槽数据来进行不同的方式展现和填充插槽内容。
举个例子

父组件:
<template>
<div class="home">
<h4>效果一:显示代办列表时,已完成的任务为绿色</h4>
<Son :todolists="todolists"> <!-- 给子组件传递数据 -->
<template slot-scope="item">
<span :style="{backgroundColor:item.item.isComplete?'green':''}">父:{
{item.item.text}}</span>
</template>
</Son>
<h4>效果二:显示代办列表时,带序号,代办列表为蓝绿搭配</h4>
<Son1 :todolists="todolists"> <!-- 给子组件传递数据 -->
<template slot-scope={item,$index}>
<span :style="{backgroundColor:item.isComplete?'green':'blue'}">父:{
{$index}}---{
{item.text}}</span>
</template>
</Son1>
</div>
</template>
<script>
import Son from './Son.vue'
import Son1 from './Son1.vue'
export default {
name: 'Father',
components:{
Son,
Son1
},
data () {
return {
todolists:[
{id:1,text:'安全管理电视电话会议',isComplete:false},
{id:2,text:'部门小组生产计划',isComplete:false},
{id:3,text:'编制计划任务分工表,分配任务',isComplete:true},
{id:4,text:'遗留问题通报到组整改,检查整改成果',isComplete:false}
]
}
}
}
</script>子组件 Son
<template>
<div class="about">
子组件
<ul>
<li v-for="(todolist,index) in todolists" :key="index">
子组件回传==>
<slot :item="todolist"></slot> <!-- 作用域插槽 -->
</li>
</ul>
</div>
</template>
<script>
export default {
props:{
todolists:[]
}
}
</script>子组件Son1
<template>
<div class="about">
子组件
<ul>
<li v-for="(todolist,index) in todolists" :key="index">
子组件回传==>
<slot :item="todolist" :$index="index"></slot> <!-- 作用域插槽 -->
</li>
</ul>
</div>
</template>
<script>
export default {
props:{
todolists:[]
}
}
</script>边栏推荐
- 直播间搭建,按钮左滑出现删除等操作按钮
- Has your phone ever been monitored?
- 【集训DAY3】挖金矿【二分答案】
- 用哈希简单封装unordered_map和unordered_set
- Gold Warehouse Database KingbaseGIS User Manual (6.2. Management Functions)
- LiveData : Transformations.map和 Transformations.switchMap用法
- What are the Shenzhen fortress machine manufacturers?Which one do you recommend?
- Eureka protects itself
- ES6 从入门到精通 # 14:迭代器 Iterator 的用法
- Wireshark classic practice and interview 13-point summary
猜你喜欢

Qt 之 QDateEdit 和 QTimeEdit

多商户商城系统功能拆解24讲-平台端分销会员

Gartner全球集成系统市场数据追踪,超融合市场增速第一

阿里云短信服务开通

深入理解多线程(第一篇)

Mysql/stonedb - slow SQL - 2022-08-09 Q16 analysis

SRv6 performance measurement

Golden Warehouse Database KingbaseGIS User Manual (6.4. Geometry Object Access Function)

Live Preview | ICML 2022 11 first-author scholars share online neural network, graph learning and other cutting-edge research

恭喜获奖得主 | 互动有礼获赠 Navicat Premium
随机推荐
【C语言】指针和数组的深入理解(第四期)
位图的基本原理以及应用
【SSL集训DAY2】Sort【树状数组】
conda新建环境时报错NotWritableError: The current user does not have write permissions
Digital wallets, red sea ecological rapid introduction of small programs can help capture device entry wisdom
第十五章 mysql存储过程与存储函数课后练习
YOLOV5 study notes (7) - training your own data set
Filament-Material 绘制基本图形
GoLang 使用 goroutine 停止的几种办法
ES6 从入门到精通 # 15:生成器 Generator 的用法
工程 (七) ——PolarSeg点云语义分割
Click: 377. Combined Sum Ⅳ
68. qt quick-qml multi-level folding drop-down navigation menu supports dynamic add/unload, support qml/widget loading, etc.
Distributed database problem (3): data consistency
Gartner全球集成系统市场数据追踪,超融合市场增速第一
Live Preview | ICML 2022 11 first-author scholars share online neural network, graph learning and other cutting-edge research
【集训DAY5】选数字【数学】
数据库优化 | 干货
Dry goods!Towards robust test-time adaptation
61.【快速排序法详解】